WPF TreeView选择 [英] WPF TreeView Selection

查看:104
本文介绍了WPF TreeView选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我想创建一个树状视图,其行为方式如下:如果单击树状视图中的某个项目,则该项目将被选中.如果我将Treeview与TextBlocks一起使用,则这是默认行为.但是,如果我改用TextBoxes,则会丢失行为.如果我单击项目的边框,则将仅选择该项目.我使用以下代码修复此问题:

Hi!

I would like to create a treeview, that behaves the following way: If I click on an item in the treeview, the item will be selected. This is the default behaviour, if I use a treeview with TextBlocks. But if I use TextBoxes instead, the behaviour is lost. The item will only be selected, if I click on the border of the item. I use the following code to repair this:

<TreeView.Resources>
    <Style TargetType="TreeViewItem">
        <Style.Triggers>
            <Trigger Property="FrameworkElement.IsKeyboardFocusWithin" Value="True">
                <Setter Property="IsSelected" Value="True" />
            </Trigger>
        </Style.Triggers>
    </Style>
</TreeView.Resources>



效果很好,但是如果我在父项之前选择一个子项,则将选择父项,并且如果我在其子项之前选择一个父项,然后再次选择父项,则不会选择父项.我真的不明白为什么.



It works quite fine, but if I select a child item before the parent, the parent will be selected, and if I select a parent before its child items and I select the parent again, the parent won''t be selected. I don''t really understand why. Could someone give a solution to this problem?

推荐答案

感谢一个很好的问题.我认为可能有几种解决方案.一种解决方案是在后面的代码中找到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;
    }
}


这篇关于WPF TreeView选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆