在DataTemplate中显示和聚焦TextBox

gre*_*nis 3 wpf textbox visibility focus

我已经搜索过高低,但我无法想出这个.我正在构建一个ListBox具有可编辑项目的项目.我有一个DataTemplateListBox.ItemTemplate包含(除其他事项外)一TextBlockTextBox.将TextBlock始终是可见的,并且TextBox是在用户双击后,才可见TextBlock.当用户单击列表中的另一个项目时,TextBox再次隐藏以显示该项目TextBlock.所有这一切都很棒.看我的代码:

XAML

<Window.Resources>
   <local:GoalCollection x:Key="goals"/>
   <DataTemplate x:Key="GoalItemTemplate" DataType="local:Goal">
      <Grid>
         <TextBlock Text="{Binding Title}"
                    MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"
                    VerticalAlignment="Center"/>
         <TextBox Name="EntryBox"
                  Text="{Binding Title}"
                  Visibility="Hidden"
                  BorderBrush="{x:Null}"
                  Padding="-2,0,0,0"
                  Panel.ZIndex="1"
                  Margin="-2,0,0,0"/>
      </Grid>
   </DataTemplate>
</Window.Resources>
<Grid>
   <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition Width="2*" />
   </Grid.ColumnDefinitions>
   <ListBox Name="GoalsList"
      ItemsSource="{Binding Source={StaticResource goals}}"
      HorizontalContentAlignment="Stretch"
      ItemTemplate="{StaticResource GoalItemTemplate}"
      SelectionChanged="GoalsList_SelectionChanged" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

C#

public partial class MainWindow : Window
{
    GoalCollection goals;
    public MainWindow()
    {
       InitializeComponent();
    }

    private childItem FindVisualChild<childItem>(DependencyObject obj)
    where childItem : DependencyObject { ... }

    protected override void OnInitialized(EventArgs e)
    {
       base.OnInitialized(e);
       goals = (GoalCollection)Resources["goals"];
    }

    private void TextBlock_MouseLeftButtonDown(object sender, 
                                               MouseButtonEventArgs e)
    {
       if (e.ClickCount == 2)
       {
          TextBlock tblk = sender as TextBlock;
          if (tblk == null) 
             return;
          TextBox tbx = ((Grid)tblk.Parent).FindName("EntryBox") as TextBox;
          if (tbx == null) 
             return;
          tbx.Visibility = Visibility.Visible;
          Keyboard.Focus(tbx);
       }
    }

    private void GoalsList_SelectionChanged(object sender, 
                                            SelectionChangedEventArgs e)
    {
       ListBoxItem lbi;
       ContentPresenter cp;
       DataTemplate dt;
       TextBox tbx;

       foreach (Goal item in e.RemovedItems)
       {
          lbi = (ListBoxItem)GoalsList.ItemContainerGenerator.
                                       ContainerFromItem(item);
          cp = FindVisualChild<ContentPresenter>(lbi);
          dt = cp.ContentTemplate;
          tbx = (TextBox)dt.FindName("EntryBox", cp);
          if (tbx == null) 
             continue;
          tbx.Visibility = Visibility.Hidden;
       }
    }
 }
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是双击后TextBox立即将焦点转移回主机ListBoxItem.需要额外的(第三次)单击以关注TextBox.

通过这个追踪,我发现TextBox确实得到了关注.但它会立即失去它(尝试为TextBox.LostKeyboardFocus事件添加一个处理程序并逐步执行`TextBlock_MouseLeftButtonDown()'方法).有任何想法吗?

谢谢.

Ada*_*ney 5

我的猜测是,click事件冒泡到了ListBox,它通过选择项目来处理它.

尝试将此添加到Click事件处理程序(之后Keyboard.Focus(tbx);)

e.Handled = true;
Run Code Online (Sandbox Code Playgroud)