HierarchicalDataTemplate
生活随笔
收集整理的這篇文章主要介紹了
HierarchicalDataTemplate
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
針對具有分層數據結構的控件設計的,比如說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(寫路徑,否則找不到)
轉載于:https://www.cnblogs.com/Lulus/p/8157718.html
總結
以上是生活随笔為你收集整理的HierarchicalDataTemplate的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 记一次支付系统的设计体验
- 下一篇: DeepMind详解新WaveNet:比