解析WPF綁定階層資料的應用詳解

來源:互聯網
上載者:User

在實際項目應用中會存在多種類型的階層資料,WPF提供了良好的資料繫結機制。其中運用最頻繁的就是ListBox和TreeView控制項。

一、ListBox和TreeView控制項的區別
1.ListBox顯示單層次資料集合,TreeView可以顯示單層次和多層次資料集合;
2.通過ListBox在UI層面可以展示良好的資料顯示效果,對資料集合可以進行排序、分組、過濾操作;
3.TreeView顯示為一個多層次的資料集合為樹形結構,通過Templete和Style屬性同樣可以為其定義良好的資料顯示效果;

二、ListBox控制項樣本
1.ListBox綁定資料進行分組:
使用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>

這樣就可以建立出類似WINDOWS 檔案管理工具的效果:

2.Listbox一些使用經驗:
/1 如果要實作類別似WINDOWS的漂亮的介面效果並進行分組,需要自訂GroupStyle的樣式,否則WPF會使用內建的GroupStyle,也可以引用GroupStyle.Default靜態屬性。
/2 ListBox只能定義一層資料結構,在ListBox中的Item裡再次使用ListBox,後ListBox裡的ItemSource不會繼承上一層ListBox的Item源中的資料集合,如有如下資料集合:複製代碼 代碼如下: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>的資料集合,其Item中的ListBox Binding List<Box>,則Item中的ListBox是無法擷取List<Box>這個資料集合的;

三、TreeView控制項樣本
1.有如上資料集合,使用TreeView綁定多層資料集合:複製代碼 代碼如下:代碼
<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屬性為層級資料範本,它繼承資料集合的層級結構,要表示樹的層級依賴關係必須使用HierarchicalDataTemplate。
屬性綁定資料使用TwoWay是為雙向屬性,當來源資料或目標被改變是更新另一方的資料。在層次樹表示中的典型應用就是:用CheckBox進行子節點的選中和未選中的狀態傳遞。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.