走進WPF之數據模板


隨着科技的進步,數據的展示形式越來越多樣化,正所謂:橫看成嶺側成峰,遠近高低各不同。同樣的數據內容,在DataGrid中的展示是文本的列表形式,在ComboBox中是下拉框的形式。給數據披上外衣,將數據和形式解耦,是一種新的發展趨勢。本文主要以一些簡單的小例子,簡述數據模板的簡單應用,僅供學習分享使用,如有不足之處,還請指正。

數據模板基礎示例【DataGrid】

DataGrid是可以自定義網格數據顯示的控件,通過自定義顯示的列模板,可以實現各式各樣的展現方式。列定義如下:

  1. DataGrid的列定義,通過Binding="{Binding Name}"的方式綁定屬性,通過ElementStyle="{StaticResource one_center}"的方式綁定樣式。
  2. DataGrid預制了幾種列展示數據的方式,如:DataGridTextColumn【文本】,DataGridCheckBoxColumn【復選框】,DataGridComboBoxColumn【下拉框】,DataGridHyperlinkColumn【鏈接】等,如果使用數據模板,則采用DataGridTemplateColumn進行定義。

UI示例如下所示:

 1 <Window x:Class="WpfApp2.A1Window"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfApp2"
 7         mc:Ignorable="d"
 8         Title="數據模板示例" Height="450" Width="650">
 9     <Window.Resources>
10         <Style x:Key="one_center" TargetType="TextBlock">
11             <Setter Property="VerticalAlignment" Value="Center"></Setter>
12             <Setter Property="HorizontalAlignment" Value="Center"></Setter>
13         </Style>
14         <Style x:Key="one_header" TargetType="DataGridColumnHeader">
15             <Setter Property="VerticalAlignment" Value="Center"></Setter>
16             <Setter Property="HorizontalAlignment" Value="Center"></Setter>
17             <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
18             <Setter Property="BorderThickness" Value="0"></Setter>
19         </Style>
20     </Window.Resources>
21     <Grid>
22         <DataGrid x:Name="one"  Margin="10" AutoGenerateColumns="False"  CanUserAddRows="False"  CanUserSortColumns="False" BorderThickness="0" >
23             <DataGrid.Columns>
24                 <DataGridTextColumn Header="姓名" Binding="{Binding Name}" Width="*" ElementStyle="{StaticResource one_center}" HeaderStyle="{StaticResource one_header}"  />
25                 <DataGridTextColumn Header="年齡" Binding="{Binding Age}" Width="*" ElementStyle="{StaticResource one_center}" HeaderStyle="{StaticResource one_header}"/>
26                 <DataGridTextColumn Header="性別" Binding="{Binding Sex}" Width="*" ElementStyle="{StaticResource one_center}" HeaderStyle="{StaticResource one_header}"/>
27                 <DataGridTextColumn Header="班級" Binding="{Binding Classes}" Width="*" ElementStyle="{StaticResource one_center}" HeaderStyle="{StaticResource one_header}"/>
28                 <DataGridTemplateColumn Header="操作" Width="*" HeaderStyle="{StaticResource one_header}">
29                     <DataGridTemplateColumn.CellTemplate>
30                         <DataTemplate>
31                             <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
32                                 <Button x:Name="edit" Content="編輯" Width="60" Margin="3" Height="25"></Button>
33                                 <Button x:Name="delete" Content="刪除" Width="60" Margin="3" Height="25"></Button>
34                             </StackPanel>
35                         </DataTemplate>
36                     </DataGridTemplateColumn.CellTemplate>
37                 </DataGridTemplateColumn>
38             </DataGrid.Columns>
39         </DataGrid>
40     </Grid>
41 </Window>

后台數據綁定

后台數據綁定通過ItemsSource進行賦值,綁定的數據的屬性名,要和DataGrid的列綁定數據的名稱保持一致,如下所示:

 1 namespace WpfApp2
 2 {
 3     /// <summary>
 4     /// A1Window.xaml 的交互邏輯
 5     /// </summary>
 6     public partial class A1Window : Window
 7     {
 8         public A1Window()
 9         {
10             InitializeComponent();
11 
12             List<Student> lst = new List<Student>();
13             lst.Add(new Student() { Name = "張三", Age = 22, Sex = "", Classes = "一班" });
14             lst.Add(new Student() { Name = "李四", Age = 21, Sex = "", Classes = "二班" });
15             lst.Add(new Student() { Name = "王五", Age = 20, Sex = "", Classes = "一班" });
16             lst.Add(new Student() { Name = "劉大", Age = 19, Sex = "", Classes = "三班" });
17             lst.Add(new Student() { Name = "麻子", Age = 18, Sex = "", Classes = "四班" });
18             one.ItemsSource = lst;
19         }
20     }
21 
22 
23     public class Student
24     {
25         public string Name { get; set; }
26 
27         public int Age { get; set; }
28 
29         public string Sex { get; set; }
30 
31         public string Classes { get; set; }
32     }
33 }

DataGrid示例,如下所示:

 

 數據模板基礎示例【ListBox和ComboBox】

ListBox,ComboBox,均是包含可選擇的項的列表,只是ListBox不需要下拉顯示,ComboBox需要下拉顯示。通過定義數據模板,可以豐富數據的展示形式。

通過ItemTemplate="{StaticResource item_template}"的形式,進行數據模板的綁定。如下所示:

 1 <Window x:Class="WpfApp2.A2Window"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfApp2"
 7         mc:Ignorable="d"
 8         Title="數據模板示例" Height="450" Width="800">
 9     <Window.Resources>
10         <DataTemplate x:Key="item_template">
11             <StackPanel Orientation="Horizontal" Margin="5 ,0">
12                 <Border Width="10" Height="10" Background="{Binding Code}"></Border>
13                 <TextBlock Text="{Binding Code}" Margin="5,0" ></TextBlock>
14             </StackPanel>
15         </DataTemplate>
16     </Window.Resources>
17     <Grid>
18         <StackPanel Margin="3" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
19             <ComboBox x:Name="one" Height="25" Width="120" Margin="5" ItemTemplate="{StaticResource item_template}"></ComboBox>
20             <ListBox x:Name="two"  Width="120" Margin="5" ItemTemplate="{StaticResource item_template}"></ListBox>
21         </StackPanel>
22     </Grid>
23 </Window>

后台數據綁定

與DataGrid一樣,后台通過ItemsSource進行數據的綁定。如下所示:

 1 namespace WpfApp2
 2 {
 3     /// <summary>
 4     /// A2Window.xaml 的交互邏輯
 5     /// </summary>
 6     public partial class A2Window : Window
 7     {
 8         public A2Window()
 9         {
10             InitializeComponent();
11             List<Color> lst = new List<Color>();
12             lst.Add(new Color() { Code = "#FE8C00" });
13             lst.Add(new Color() { Code = "#1F7F50" });
14             lst.Add(new Color() { Code = "#AA8C00" });
15             lst.Add(new Color() { Code = "#FEAA00" });
16             lst.Add(new Color() { Code = "#008CAA" });
17             lst.Add(new Color() { Code = "#FEBB00" });
18             one.ItemsSource = lst;
19             two.ItemsSource = lst;
20         }
21     }
22 
23     public class Color
24     {
25         public string Code { get; set; }
26     }
27 }

示例截圖,如下所示:

 

數據模板基礎示例【ItemsControl】

ItemsControl,主要用於展示集合數據的項,也是列表控件的一種。ItemsControl 需要設置兩個內容:

  • ItemsControl.ItemsPanel,做為數據展示的容器。
  • ItemsControl.ItemTemplate,用於單個數據的展示形式。

具體如下所示:

 1 <Window x:Class="WpfApp2.A3Window"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfApp2"
 7         mc:Ignorable="d"
 8         Title="A3Window" Height="450" Width="800">
 9     <Grid>
10         <ItemsControl x:Name="one">
11             <ItemsControl.ItemsPanel>
12                 <ItemsPanelTemplate>
13                     <WrapPanel></WrapPanel>
14                 </ItemsPanelTemplate>
15             </ItemsControl.ItemsPanel>
16             <ItemsControl.ItemTemplate>
17                 <DataTemplate>
18                     <Button Width="50" Height="50" Margin="5" Content="{Binding Code}"></Button>
19                 </DataTemplate>
20             </ItemsControl.ItemTemplate>
21         </ItemsControl>
22     </Grid>
23 </Window>

后台數據綁定

與DataGrid一樣,后台通過ItemsSource進行數據的綁定。如下所示:

 1 namespace WpfApp2
 2 {
 3     /// <summary>
 4     /// A3Window.xaml 的交互邏輯
 5     /// </summary>
 6     public partial class A3Window : Window
 7     {
 8         public A3Window()
 9         {
10             InitializeComponent();
11             List<Test> lst = new List<Test>();
12             lst.Add(new Test() { Code = "1" });
13             lst.Add(new Test() { Code = "2" });
14             lst.Add(new Test() { Code = "3" });
15             lst.Add(new Test() { Code = "4" });
16             lst.Add(new Test() { Code = "5" });
17             lst.Add(new Test() { Code = "6" });
18             one.ItemsSource = lst;
19         }
20     }
21 
22     public class Test
23     {
24         public string Code { get; set; }
25     }
26 }

示例截圖

 

 備注

數據模板的應用場景還有很多,本文旨在拋磚引玉,共同學習,一起進步。

青玉案·凌波不過橫塘路

【作者】賀鑄  【朝代】宋

凌波不過橫塘路。但目送、芳塵去。錦瑟華年誰與度。月橋花院,瑣窗朱戶。只有春知處。

飛雲冉冉蘅皋暮。彩筆新題斷腸句。若問閑情都幾許。一川煙草,滿城風絮。梅子黃時雨。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM