gpt4 book ai didi

c# - 我怎么知道一个 Silverlight 控件已经显示出来了?

转载 作者:行者123 更新时间:2023-11-30 22:38:39 26 4
gpt4 key购买 nike

我有一个列表框,显示可以添加和更改的帮助主题的名称。最初它只是显示字符串,但为了使内联编辑正常工作,我将其更改为使用由字符串和 InEdit 组成的自定义类型。属性,以便 UI 可以确定是否显示 TextBlockTextBox :

XAML:

<ListBox ItemsSource="{Binding HelpTopics, Mode=TwoWay}"
SelectedValuePath="Description"
SelectedValue="{Binding SelectedPageId, Mode=TwoWay}"
SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Description, Mode=TwoWay}"
VerticalAlignment="Center"
MouseLeftButtonUp="TopicTextBlock_MouseLeftButtonUp"
Visibility="{Binding InEdit, Converter={StaticResource boolToVisibilityConverter}, ConverterParameter=contra}"/>

<TextBox Text="{Binding Description, Mode=TwoWay}"
Visibility="{Binding InEdit, Converter={StaticResource boolToVisibilityConverter}, ConverterParameter=pro}"
LostFocus="EditTopicTextBox_LostFocus"
HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

<Button Margin="5" Content="Add Topic" Command="{Binding AddTopicCommand}"/>

HelpTopics是一个 ObservableCollection<EditableHelpTopic> .
SelectedPageIdstring .
boolToVisibilityConverter是一个按照它说的去做的转换器。

什么有效:

  • 添加主题会创建一个新项目并将其添加到列表中,并将该项目置于编辑模式。
  • 双击现有项目可将该项目置于编辑模式,将焦点设置到 TextBox并选择所有文本以便将其覆盖。
  • TextBox失去焦点编辑被保存并且显示返回到TextBlock .

什么不起作用:

  • 添加新主题时 TextBox应该有焦点和选定的文本,以便用户可以输入新名称。

所以我的问题是代码或事件中是否有一点我知道 TextBox已创建并可见,因此我可以设置焦点并选择其内容。我试过连接到 SelectionChanged事件,但当它触发 TextBox 时尚未显示。我还向 OnAddTopicExecute 添加了一个事件我在 View 中处理的 View 模型中的方法,但再次在 TextBox 之前触发可见。


下面是支持上述 XAML 的代码。我已经尽量删减了,但是好像还是很多,不感兴趣可以跳过 ;)

代码隐藏:

private DateTime lastClickTime = DateTime.MinValue;
private Point lastClickPosition;

private void TopicTextBlock_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
UIElement element = sender as UIElement;

if ((DateTime.Now - this.lastClickTime).TotalMilliseconds > 300)
{
this.lastClickPosition = e.GetPosition(element);
this.lastClickTime = DateTime.Now;
}
else
{
Point position = e.GetPosition(element);
if (Math.Abs(this.lastClickPosition.X - position.X) < 4 && Math.Abs(this.lastClickPosition.Y - position.Y) < 4)
{
var textBlock = sender as TextBlock;
var editableHelpTopic = textBlock.DataContext as EditableHelpTopic;
editableHelpTopic.InEdit = true;
var parent = textBlock.Parent as Grid;
TextBox textBox = parent.Children.First(c => c.GetType() == typeof(TextBox)) as TextBox;
textBox.Focus();
textBox.SelectAll();
}
}
}

private void EditTopicTextBox_LostFocus(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
var editableHelpTopic = textBox.DataContext as EditableHelpTopic;
editableHelpTopic.InEdit = false;

if (!textBox.Text.Equals(editableHelpTopic.Description))
{
this.editViewModel.RenameTopic(textBox.Text);
}
}

查看模型:

public EditViewModel()
{
...
this.AddTopicCommand = new DelegateCommand(this.OnAddTopicExecute, this.OnAddTopicCanExecute);
...
}

哪里DelegateCommandICommand 的一个实现.

private void OnAddTopicExecute(object parameter)
{
var newTopic = new EditableHelpTopic
{
Description = "NewTopic",
InEdit = true
};
this.HelpTopics.Add(newTopic);
this.SelectedPageId = newTopic.Description;
}

定义:

public class EditableHelpTopic : INotifyPropertyChanged
{
public bool InEdit { ... }
public string Description { ... }
}

最佳答案

结果比我想象的要简单。

我只需要向 TextBox 添加一个 Loaded 事件处理程序:

private void EditTopicTextBox_Loaded(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
var editableHelpTopic = textBox.DataContext as EditableHelpTopic;
if (editableHelpTopic.InEdit)
{
textBox.Focus();
textBox.SelectAll();
}
}

关于c# - 我怎么知道一个 Silverlight 控件已经显示出来了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6016141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com