【发布时间】:2018-04-19 11:40:31
【问题描述】:
我的 WPF 窗口中有 TreeView。 TreeViews DataContext 绑定到 XDocument。
我正在寻找从后面的代码中设置 selectedItem。例如 .ItemContainerGenerator.Items 返回的节点是 System.Xml.Linq.XElement 类型而不是 System.Windows.Controls.TreeViewItem 我不能只设置 isSelected :-(
有人知道如何解决这个问题吗?
编辑: XAML 代码:
<Window.Resources>
<DataTemplate x:Key="AttributeTemplate">
<StackPanel Orientation="Horizontal"
Margin="3,0,0,0"
HorizontalAlignment="Center">
<TextBlock Text="{Binding Path=Name}"
Foreground="{StaticResource xmAttributeBrush}" FontFamily="Consolas" FontSize="8pt" />
<TextBlock Text="=""
Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
<TextBlock Text="{Binding Path=Value, Mode=TwoWay}"
Foreground="{StaticResource xmlValueBrush}" FontFamily="Consolas" FontSize="8pt" />
<TextBlock Text="""
Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="NodeTemplate">
<StackPanel Orientation="Horizontal" Focusable="False">
<TextBlock x:Name="tbName" Text="Root" FontFamily="Consolas" FontSize="8pt" />
<ItemsControl
ItemTemplate="{StaticResource AttributeTemplate}" HorizontalAlignment="Center"
ItemsSource="{Binding Converter={StaticResource AttrConverter}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
<HierarchicalDataTemplate.ItemsSource>
<Binding Path="Elements" />
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=NodeType}" Value="Element" />
<Condition Binding="{Binding Path=FirstNode.FirstNode.NodeType}" Value="Text" />
</MultiDataTrigger.Conditions>
<Setter TargetName="tbName" Property="Text" >
<Setter.Value>
<Binding Path="Name" />
</Setter.Value>
</Setter>
<Setter TargetName="tbName" Property="ToolTip">
<Setter.Value>
<MultiBinding Converter="{StaticResource MultiString2StringKonverter}">
<Binding Path="FirstNode.Value" />
<Binding Path="FirstNode.NextNode.Value" />
</MultiBinding>
</Setter.Value>
</Setter>
</MultiDataTrigger>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=FirstNode.NodeType}" Value="Text">
<Setter TargetName="tbName" Property="Text">
<Setter.Value>
<MultiBinding StringFormat="{}{0} = {1}">
<Binding Path="Name"/>
<Binding Path="FirstNode.Value"/>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</Window.Resources>
<TreeView x:Name="LizenzAnsicht"
ItemsSource="{Binding Path=Root.Elements, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{StaticResource ResourceKey=NodeTemplate}"
/>
代码背后: ...
LizenzAnsicht.DataContext = XDocument.Load(<Path to XML-File>);
【问题讨论】:
-
看起来您正在返回项目而不是树节点:ItemContainerGenerator.Items。为什么不返回 ItemContainerGenerator
-
用你的代码改进你的问题。那太棒了!
-
@jdweng:我认为 ItemContainerGenerator 不会给我一个具有所需属性或方法的对象。我想我需要遍历 TreeView 的所有项目,直到找到正确的项目......这个需要作为具有 isEnabled 属性的对象......
标签: c# xml wpf treeview linq-to-xml