TreeView.ItemContainerGenerator.ContainerFromItem返回null非根项目。解决方法? [英] TreeView.ItemContainerGenerator.ContainerFromItem returns null for nonroot items. Workaround?
问题描述
在下面的示例中,当我选择字符串,在窗口的标题变为空。但我的必须的获得字符串的容器。具体来说,我想要做的的SelectedItem = NULL
相当于(但该属性是只读的 TreeView控件
,所以我M好一会容器设置其 IsSelected
到假
)。 ?怎么办
<窗口x:类=WpfApplication1.Window1的xmlns =http://schemas.microsoft .COM / WinFX的/ 2006 / XAML /演示的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml>
< Window.Resources>
< X:数组的xmlns =CLR的命名空间:系统;装配= mscorlib程序X:键=阵列TYPE =对象>
< X:ArrayExtension类型=对象>
<串GT;串LT; /字符串>
< / X:ArrayExtension>
< / X:数组>
< /Window.Resources>
<树视图的ItemsSource ={StaticResource的阵列}SelectedItemChanged =处理程序>
< TreeView.ItemTemplate>
< HierarchicalDataTemplate的ItemsSource ={结合}>
< TextBlock的文本=阵列/>
< HierarchicalDataTemplate.ItemTemplate>
<&DataTemplate的GT;
< TextBlock的文本={结合}/>
< / DataTemplate中>
< /HierarchicalDataTemplate.ItemTemplate>
< / HierarchicalDataTemplate>
< /TreeView.ItemTemplate>
< / TreeView的>
命名空间WpfApplication1
{
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent();
}
私人无效处理程序(对象发件人,EventArgs五)
{
变种的TreeView =发件人为树视图;
VAR容器= treeView.ItemContainerGenerator.ContainerFromItem(treeView.SelectedItem);
标题=容器!= NULL? container.ToString():空;
}
}
}
的问题是,每个树型视图
本身就是一个的ItemsControl
,使他们各自管理自己的容器为他们的孩子
然而,有一个非常简单的方法做你想做的事:不是注册到 SelectedItemChanged
事件中,注册到 TreeViewItem.Selected
事件,将冒泡与 OriginalSource
设置为选定<$ C $ 。C>树型视图
XAML:
<树视图的ItemsSource ={StaticResource的阵列}TreeViewItem.Selected =TreeViewItem_Selected>
后面的代码:
私人无效TreeViewItem_Selected(对象发件人,RoutedEventArgs E){
的TreeViewItem容器=(树型视图)e.OriginalSource;
标题=容器!= NULL? container.ToString():空;
}
In the following sample, when I select "String", the title of the window turns to "null". But I must obtain the container of "String". Specifically, I want to do the equivalent of SelectedItem = null
(but that property is read only for TreeView
, so I'm trying to get to the container to set its IsSelected
to false
). What to do?
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<x:Array xmlns="clr-namespace:System;assembly=mscorlib" x:Key="Array" Type="Object">
<x:ArrayExtension Type="Object">
<String>String</String>
</x:ArrayExtension>
</x:Array>
</Window.Resources>
<TreeView ItemsSource="{StaticResource Array}" SelectedItemChanged="Handler">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding}">
<TextBlock Text="Array"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Handler(object sender, EventArgs e)
{
var treeView = sender as TreeView;
var container = treeView.ItemContainerGenerator.ContainerFromItem(treeView.SelectedItem);
Title = container != null ? container.ToString() : "null";
}
}
}
The issue is that each TreeViewItem
is itself an ItemsControl
so they each manage their own containers for their children.
However, there's a very simple way to do what you want to do: instead of registering to the SelectedItemChanged
event, register to the TreeViewItem.Selected
event that will bubble up with the OriginalSource
set to the selected TreeViewItem
.
XAML:
<TreeView ItemsSource="{StaticResource Array}" TreeViewItem.Selected="TreeViewItem_Selected">
Code behind:
private void TreeViewItem_Selected(object sender, RoutedEventArgs e) {
TreeViewItem container = (TreeViewItem) e.OriginalSource;
Title = container != null ? container.ToString() : "null";
}
这篇关于TreeView.ItemContainerGenerator.ContainerFromItem返回null非根项目。解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!