无法阻止TreeView使用Grid类设置SelectedItem [英] Cannot prevent TreeView from setting SelectedItem with Grid class

查看:62
本文介绍了无法阻止TreeView使用Grid类设置SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义TreeViewItem,它可以防止在默认情况下选择自己,如下面的代码中所定义:

 < ;   Window.Resources  >  
< 样式 x:Key = Derived_TreeStyle TargetType = {x:输入local:Derived_TreeViewItem} BasedOn = < span class =code-keyword> {StaticResource {x:Type TreeViewItem}} >
< Setter Property = 可聚焦 Value = false / >
< / Style >
< / Window.Resources >



设置属性可附加的每个项目的可聚焦为false适用于除网格类型之外的所有内容。



例如:

 Derived_TreeViewItem item =  new  Derived_TreeViewItem(); 

Grid grid = new Grid();

grid.Focusable = false ; // 不起作用

item.Items.Add(grid);



我还验证了SelectedItem是Grid,因此在选择项目后不可能在项目上设置IsSelected属性。



还有哪些其他方法可以防止TreeView选择其中的任何项目?

解决方案

忽略该对象的对象在 TreeViewItem.Items 内的可聚焦属性是任何不需要以任何方式聚焦的对象,例如 StackPanel TextBlock 网格等。



我仍​​然不知道为什么 TreeView 将重点关注并选择这些类型,但似乎解决方案是做以下内容:

 Derived_TreeViewItem item =  new  Derived_TreeViewItem(); 

Grid grid = new Grid();

item.Header = grid;



这确保附加的对象保留 TreeViewItem.Style


I created a custom TreeViewItem that prevents itself from being selected by default as defined in the following code:

<Window.Resources>
    <Style x:Key="Derived_TreeStyle" TargetType="{x:Type local:Derived_TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
        <Setter Property="Focusable" Value="false" />
    </Style>
</Window.Resources>


Setting the property Focusable to false for each item attached works for everything except for the Grid type.

For example:

Derived_TreeViewItem item = new Derived_TreeViewItem();

Grid grid = new Grid();

grid.Focusable = false;  // does not work

item.Items.Add( grid );


I also verified that the SelectedItem was the Grid, so it is impossible to set the IsSelected property on the item after it is selected.

What other methods are there that I could use to prevent the TreeView from selecting any items within it?

解决方案

The objects that ignore the Focusable property while within the TreeViewItem.Items are any object that does not require focus in any way, e.g. StackPanel, TextBlock, Grid, etc.

I still have no idea why the TreeView will focus and select these types, but it would seem the solution is to do the following:

Derived_TreeViewItem item = new Derived_TreeViewItem();
 
Grid grid = new Grid();
 
item.Header = grid;


This ensures the attached Object retains the TreeViewItem.Style.


这篇关于无法阻止TreeView使用Grid类设置SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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