HierarchicalDataTemplate
生活随笔
收集整理的這篇文章主要介紹了
HierarchicalDataTemplate
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
針對(duì)具有分層數(shù)據(jù)結(jié)構(gòu)的控件設(shè)計(jì)的,比如說(shuō)TreeView,相當(dāng)于可以每一個(gè)層級(jí)上做DataTemplate
XmlDataProvider:數(shù)據(jù)源,寫在Resources下
<XmlDataProvider x:Key="Info" XPath="Nations"> <x:XData> <Nations xmlns=""> <Nation Name="中國(guó)"> <Provinces> <Province Name="安徽"> <Citys> <City Name="安慶"> <Countrys> <Country Name="潛山"/> <Country Name="桐城"/> </Countrys> </City> <City Name="合肥"> <Countrys> <Country Name="長(zhǎng)豐"/> <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:層級(jí)模板,寫在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表示定義的目標(biāo)是Nation
ItemsSource表示下一級(jí)是Provinces/Province (總標(biāo)簽/單個(gè)標(biāo)簽名)
StackPanel 定義Nation的外觀
XPath=@Name表示綁定為Name屬性
比如:
<Nation Name="中國(guó)" 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>像引用靜態(tài)資源一樣使用
XPath決定顯示的根節(jié)點(diǎn)
如果想從第二/三級(jí)開始顯示,而不是根節(jié)點(diǎn)
修改XPath(寫路徑,否則找不到)
轉(zhuǎn)載于:https://www.cnblogs.com/Lulus/p/8157718.html
總結(jié)
以上是生活随笔為你收集整理的HierarchicalDataTemplate的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 记一次支付系统的设计体验
- 下一篇: DeepMind详解新WaveNet:比