2011-05-16 66 views
2

我有一个列表框,显示可添加的帮助主题的名称以及更改的主题的名称。本来这只是显示的字符串,但要获得行内编辑工作,我改变了它使用由字符串的自定义类型和InEdit属性,以便用户界面可以判断是否显示TextBlockTextBox我如何知道已经显示Silverlight控件?

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}"/> 

HelpTopicsObservableCollection<EditableHelpTopic>
SelectedPageIdstring
boolToVisibilityConverter是一个完全按照它说的转换器。

什么工作:

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

什么不起作用:

  • 当添加了一个新的话题TextBox应重点和文本选择,使用户可以输入新的名称有。

所以我的问题是是那里的代码或事件,我知道TextBox已创建并可见,所以我可以设置对焦并选择其内容的点。我尝试过登录SelectionChanged事件,但当发生这种情况时,TextBox尚未显示。我还在视图模型中为视图模型中的OnAddTopicExecute方法添加了一个事件,但在看到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); 
    ... 
} 

DelegateCommand其中的是一个ICommand implemetation。

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 { ... } 
} 

回答

3

这竟然是比我想象的简单。

我只是需要一个Loaded事件处理程序添加到TextBox:用[这]沿着

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(); 
    } 
} 
+0

+1克里斯,这个(http://stackoverflow.com/questions/124649/how-do-我给了一个textbox-focus-in-silverlight)解决了长时间运行的头痛问题:) – Town 2011-05-16 14:05:12