码迷,mamicode.com
首页 > 其他好文 > 详细

HierarchicalDataTemplate

时间:2017-12-31 15:28:30      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:font   nat   开始   resources   content   body   pack   key   post   

针对具有分层数据结构的控件设计的,比如说TreeView,相当于可以每一个层级上做DataTemplate

XmlDataProvider:数据源,写在Resources下

<XmlDataProvider x:Key="Info" XPath="Nations">
    <x:XData>
        <Nations xmlns="">
            <Nation Name="中国">
                <Provinces>
                    <Province Name="安徽">
                        <Citys>
                            <City Name="安庆">
                                <Countrys>
                                    <Country Name="潜山"/>
                                    <Country Name="桐城"/>
                                </Countrys>
                            </City>
                            <City Name="合肥">
                                <Countrys>
                                    <Country Name="长丰"/>
                                    <Country Name="肥东"/>
                                </Countrys>
                            </City>
                        </Citys>
                    </Province>
                    <Province Name="江苏">
                        <Citys>
                            <City Name="南京">
                                <Countys>
                                    <Country Name="溧水"/>
                                    <Country Name="高淳"/>
                                </Countys>
                            </City>
                            <City Name="苏州">
                                <Countys>
                                    <Country Name="常熟"/>
                                </Countys>
                            </City>
                        </Citys>
                    </Province>
                </Provinces>
            </Nation>
        </Nations>
    </x:XData>
</XmlDataProvider>

HierarchicalDataTemplate:层级模板,写在Resources下

<HierarchicalDataTemplate DataType="Nation" ItemsSource="{Binding XPath=Provinces/Province}">
    <StackPanel Background="AliceBlue">
        <TextBlock FontSize="20" Text="{Binding XPath=@Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Province" ItemsSource="{Binding XPath=Citys/City}">
    <StackPanel Background="LightBlue">
        <TextBlock FontSize="18" Text="{Binding XPath=@Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="City" ItemsSource="{Binding XPath=Countrys/Country}">
    <StackPanel Background="LightBlue">
        <TextBlock FontSize="18" Text="{Binding XPath=@Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="Country">
    <StackPanel Background="LightSalmon">
        <TextBlock FontSize="18" Text="{Binding XPath=@Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>

解释

<HierarchicalDataTemplate DataType="Nation" ItemsSource="{Binding XPath=Provinces/Province}">
    <StackPanel Background="AliceBlue">
        <TextBlock FontSize="20" Text="{Binding XPath=@Name}"/>
    </StackPanel>
</HierarchicalDataTemplate>

DataType表示定义的目标是Nation
ItemsSource表示下一级是Provinces/Province (总标签/单个标签名)
StackPanel 定义Nation的外观
XPath=@Name表示绑定为Name属性

比如:

<Nation Name="中国" Age="15">
    <HierarchicalDataTemplate DataType="Nation" ItemsSource="{Binding XPath=Provinces/Province}">
        <StackPanel Background="AliceBlue">
            <TextBlock FontSize="20" Text="{Binding XPath=@Name}"/>
            <Label FontSize="15" Content="{Binding XPath=@Age}"></Label>
        </StackPanel>
    </HierarchicalDataTemplate>
</Nation>
 
技术分享图片
 

TreeView

<TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=Info},XPath=Nation}"></TreeView>

像引用静态资源一样使用
XPath决定显示的根节点

如果想从第二/三级开始显示,而不是根节点
修改XPath(写路径,否则找不到)

<TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=Info},XPath=Nation/Provinces/Province}"></TreeView>
 
技术分享图片
 
<TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=Info},XPath=Nation/Provinces/Province/Citys/City}"></TreeView>
 
技术分享图片
 

HierarchicalDataTemplate

标签:font   nat   开始   resources   content   body   pack   key   post   

原文地址:https://www.cnblogs.com/Lulus/p/8157718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!