我有一个这种类型的XML文档。
<?xml version="1.0" encoding="utf-8"?>
<Parameters>
<Param Name="Father0_0" ID="1">
<Param0 Name="Father0_1" ID="2">
<Param1 Name="Father0_2" ID="3">
<Param2 Name="Child0_0" ID_ch="1"/>
</Param1>
<Param1 Name="Father0_3" ID="4">
<Param2 Name="Father0_4" ID="5">
<Param3 Name="Child0_3" ID_ch="2"/>
</Param2>
</Param1>
</Param0>
</Param>
<Param Name="Father1_0" ID="6">
<Param0 Name="Child1_0" ID_ch="3" />
</Param>
<Param Name="Father2_0" ID="7">
<Param1 Name="Child2_0" ID_ch="3"/>
</Param>
<Param Name="Child3_0" ID_ch="4"/>
</Parameters>
.CS
所以我加载了我的XML
XmlDataProvider _xml = FindResource("xmldata") as XmlDataProvider;
var xmlDocument = new XmlDocument();
xmlDocument.Load("34.xml");
_xml.Document = xmlDocument;
如果我按照XAML中的说明进行操作,我会在树视图中获取Chechkboks所有项目吗?
.XAML
<Window.Resources>
<XmlDataProvider x:Key="xmldata" XPath="Parameters"/>
<!--The same is done for the Param0,Param1,Param2,Param3 changing DataType=""-->
<HierarchicalDataTemplate DataType="Param" ItemsSource="{Binding XPath= ./*}">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="5,0,0,0" Content="{Binding XPath=@Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<!-------------------------------->
</Window.Resources>
<Grid>
<TreeView ItemsSource="{Binding Source={StaticResource xmldata}, XPath=Param}" />
</Grid>
如何仅为儿童制作CheckBox。
答案 0 :(得分:2)
您可以检查TreeViewItem
是否没有项目,那么这是子节点,您可以将CheckBox.Visibility
绑定为:
<HierarchicalDataTemplate DataType="Param" ItemsSource="{Binding XPath= ./*}">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="5,0,0,0" Content="{Binding XPath=@Name}" >
<CheckBox.Style>
<Style TargetType="CheckBox">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}}" Value="0">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</StackPanel>
</HierarchicalDataTemplate>
答案 1 :(得分:2)
在XML数据的情况下自定义TreeView
的更通用方法是创建自己的数据模板选择器:
public class ItemTemplateSelector : DataTemplateSelector
{
public DataTemplate ParentTemplate { get; set; }
public DataTemplate ChildTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var element = item as XElement;
if (element != null)
{
return element.Elements().Any() ? ParentTemplate : ChildTemplate;
}
return base.SelectTemplate(item, container);
}
}
以下是如何在XAML中使用它:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ItemTemplateSelector x:Key="ItemTemplateSelectorKey">
<local:ItemTemplateSelector.ParentTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Elements}">
<TextBlock Text="{Binding Attribute[Name].Value}"/>
</HierarchicalDataTemplate>
</local:ItemTemplateSelector.ParentTemplate>
<local:ItemTemplateSelector.ChildTemplate>
<DataTemplate>
<CheckBox Content="{Binding Attribute[Name].Value}"/>
</DataTemplate>
</local:ItemTemplateSelector.ChildTemplate>
</local:ItemTemplateSelector>
</Window.Resources>
<TreeView ItemsSource="{Binding Root.Elements}"
ItemTemplateSelector="{StaticResource ItemTemplateSelectorKey}"/>
</Window>
代码隐藏:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = XDocument.Parse(Properties.Resources.XmlFile);
}
}
结果如下:
请注意,我已使用XDocument
API代替XmlDocument
一个。
<强> UPD 强>
ChildTemplate
被简化了一点,以便更具可读性,但这在内容中显示下划线存在问题(例如,请参阅this答案进行解释)。