且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF TreeView选择

更新时间:2021-08-14 07:57:39

感谢一个很好的问题.我认为可能有几种解决方案.一种解决方案是在后面的代码中找到Visual Parent,然后选择treeViewitem.所以我的工作在下面给出,

XAML代码,

Thanks for a nice question. I think there could be several solutions for that. One solution could be to find the Visual Parent in the Code behind and then select the treeViewitem. So What i have done is given below,

The XAML Code,

<StackPanel>
       <TreeView>
           <TreeView.Resources>
               <Style TargetType="TreeViewItem">
                   <Setter Property="Padding" Value="5"/>
                   <Setter Property="HeaderTemplate">
                       <Setter.Value>
                           <DataTemplate>
                               <TextBox Text="{Binding Mode=OneWay}"  IsKeyboardFocusedChanged="TextBox_IsKeyboardFocusedChanged"/>
                           </DataTemplate>
                       </Setter.Value>
                   </Setter>
                   <!--<Style.Triggers>
                       <Trigger Property="IsKeyboardFocusWithin" Value="True">
                           <Setter Property="IsSelected" Value="True"/>
                       </Trigger>
                   </Style.Triggers>-->
               </Style>
           </TreeView.Resources>
           <TreeViewItem Header="One">
               <TreeViewItem Header="ChildOne"/>
           </TreeViewItem>
           <TreeViewItem Header="One">
               <TreeViewItem Header="ChildOne"/>
           </TreeViewItem>
           <TreeViewItem Header="One">
               <TreeViewItem Header="ChildOne"/>
           </TreeViewItem>
       </TreeView>
       <TreeView>
           <TreeViewItem Header="One">
               <TreeViewItem Header="ChildOne"/>
           </TreeViewItem>
           <TreeViewItem Header="One">
               <TreeViewItem Header="ChildOne"/>
           </TreeViewItem>
           <TreeViewItem Header="One">
               <TreeViewItem Header="ChildOne"/>
           </TreeViewItem>
       </TreeView>
   </StackPanel>



在后面的代码中,您需要编写一些代码,



And in the code behind you need to write some code,

private void TextBox_IsKeyboardFocusedChanged(object sender, DependencyPropertyChangedEventArgs e)
       {
           if (Convert.ToBoolean(e.NewValue) == true)
           {
               var v = FindVisualParent<TreeViewItem>(sender as UIElement);
               v.IsSelected = true;
           }

       }

       public static T FindVisualParent<T>(UIElement element) where T : UIElement
       {
           UIElement parent = element; while (parent != null)
           {
               T correctlyTyped = parent as T; if (correctlyTyped != null)
               {
                   return correctlyTyped;
               }
               parent = VisualTreeHelper.GetParent(parent) as UIElement;
           } return null;
       }




就这样.享受吧.




That''s all. Enjoy it.


我很想看到一个完整的例子.我可能建议尝试在后面尝试代码.由于这是一个很好的View问题,因此与MVVM模式保持一致.投票为问题5.
I would be interested is seeing a full example. I might suggest trying code behind instead. Since this is a View issue that should be fine, keeping with the MVVM pattern. Voted 5 for question.


感谢您的提示和代码.我最终通过以下方式向TextBox的GotKeyboardFocus事件添加了一个处理程序,它可以正常工作.

Thank you for the hint and the code. I added finally a handler to the GotKeyboardFocus event of the TextBox the following way, and it works fine.

private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    DependencyObject item = sender as DependencyObject;

    while (item != null && !(item is TreeViewItem))
    {
        item = VisualTreeHelper.GetParent(item);
    }

    if (item != null)
    {
        ((TreeViewItem)item).IsSelected = true;
    }
}