解析WPF綁定層次結(jié)構(gòu)數(shù)據(jù)的應(yīng)用詳解
在實際項目應(yīng)用中會存在多種類型的層次結(jié)構(gòu)數(shù)據(jù),WPF提供了良好的數(shù)據(jù)綁定機制。其中運用最頻繁的就是ListBox和TreeView控件。
一、ListBox和TreeView控件的區(qū)別
1.ListBox顯示單層次數(shù)據(jù)集合,TreeView可以顯示單層次和多層次數(shù)據(jù)集合;
2.通過ListBox在UI層面可以展示良好的數(shù)據(jù)顯示效果,對數(shù)據(jù)集合可以進行排序、分組、過濾操作;
3.TreeView顯示為一個多層次的數(shù)據(jù)集合為樹形結(jié)構(gòu),通過Templete和Style屬性同樣可以為其定義良好的數(shù)據(jù)顯示效果;
二、ListBox控件示例
1.ListBox綁定數(shù)據(jù)進行分組:
使用ListBox.GridStyle標簽,定義HeaderTemplate屬性用來定義組頭的外觀:
代碼
<ListBox ItemSource="{Binding Path=Data}">
<ListBox.GridStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Stackpanel>
<Image Source="xxx.jpg"/>
<Label Content="C:"/>
<Stackpanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</ListBox.GridStyle>
......
</ListBox>
這樣就可以創(chuàng)建出類似WINDOWS 文件管理器的效果:

2.Listbox一些使用經(jīng)驗:
/1 如果要實現(xiàn)類似WINDOWS的漂亮的界面效果并進行分組,需要自定義GroupStyle的樣式,否則WPF會使用內(nèi)建的GroupStyle,也可以引用GroupStyle.Default靜態(tài)屬性。
/2 ListBox只能定義一層數(shù)據(jù)結(jié)構(gòu),在ListBox中的Item里再次使用ListBox,后ListBox里的ItemSource不會繼承上一層ListBox的Item源中的數(shù)據(jù)集合,如有如下數(shù)據(jù)集合:
public List<Groups> groups = new List<Groups>();groups.Add(new Group);........
public class Group {
public int Id { get; set; }
public string Name { get; set; }
private List<Box> boxes = new List<Box>();
public List<Box> Boxes {
get { return boxes; }
}
}
Listbox的ItemSource Binding List<Groups>的數(shù)據(jù)集合,其Item中的ListBox Binding List<Box>,則Item中的ListBox是無法獲取List<Box>這個數(shù)據(jù)集合的;
三、TreeView控件示例
1.有如上數(shù)據(jù)集合,使用TreeView綁定多層數(shù)據(jù)集合:
代碼
<TreeView x:Name="maintree" FocusVisualStyle="{x:Null}" ItemsSource="{Binding Groups}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type m:GroupVO}" ItemsSource="{Binding Boxes}">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=FriendlyName}"></Label>
<CheckBox VerticalAlignment="Center" IsChecked="{Binding Path=IsSelected}"></CheckBox>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type m:BoxVO}">
<Grid Margin="0,5,5,10" MouseDown="maintree_MouseDown" Loaded="Grid_Loaded">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="6*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="/Resources/Images/shgbit.png" Width="50" VerticalAlignment="Top" Grid.Column="0" Grid.Row="0"></Image>
<Label Grid.RowSpan="2" Grid.Row="0" Grid.Column="0" Margin="5,5,0,0" Content="{Binding Path=FriendlyName}"></Label>
</DataTemplate>
</TreeView.Resources>
</TreeView>
HierarchicalDataTemplate屬性為層級數(shù)據(jù)模板,它繼承數(shù)據(jù)集合的層級結(jié)構(gòu),要表示樹的層級依賴關(guān)系必須使用HierarchicalDataTemplate。
屬性綁定數(shù)據(jù)使用TwoWay是為雙向?qū)傩?,當源?shù)據(jù)或目標被改變是更新另一方的數(shù)據(jù)。在層次樹表示中的典型應(yīng)用就是:用CheckBox進行子節(jié)點的選中和未選中的狀態(tài)傳遞。
相關(guān)文章
ASP.NET Core開發(fā)教程之Logging利用NLog寫日志文件
一直很喜歡 NLog 的簡潔和擴展性,所以下面這篇文章主要給大家介紹了關(guān)于ASP.NET Core開發(fā)教程之Logging利用NLog寫日志文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-07-07MVC項目結(jié)構(gòu)搭建及單個類的實現(xiàn)學(xué)習筆記1
這篇文章主要介紹了MVC項目結(jié)構(gòu)搭建及單個類在各個層次中的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Could not load file or assembly "App_Licenses.dll"
Could not load file or assembly "App_Licenses.dll"的問題2010-03-03asp.net textbox javascript實現(xiàn)enter與ctrl+enter互換 文本框發(fā)送消息與換行(類似
今天與大家分享一下 asp.net textbox javascript實現(xiàn)enter與ctrl+enter互換 文本框發(fā)送消息與換行(類似于QQ),這個功能到底怎么實現(xiàn)?首先聲明以下幾點2012-01-01AspNet Core上實現(xiàn)web定時任務(wù)實例
在本篇文章里小編給大家分享了關(guān)于AspNet Core上實現(xiàn)web定時任務(wù)的實例內(nèi)容,有興趣的朋友們學(xué)習參考下。2019-02-02ASP.NET Core中實現(xiàn)全局異常攔截的完整步驟
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中如何實現(xiàn)全局異常攔截的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2021-01-01ASP.NET技巧:同時對多個文件進行大量寫操作對性能優(yōu)化
ASP.NET技巧:同時對多個文件進行大量寫操作對性能優(yōu)化...2006-09-09