WPF中的數(shù)據(jù)模板用法介紹
數(shù)據(jù)模板常用在3種類型的控件, 下圖形式:

- 1.Grid這種列表表格中修改Cell的數(shù)據(jù)格式, CellTemplate可以修改單元格的展示數(shù)據(jù)的方式。
- 2.針對列表類型的控件, 例如樹形控件,下拉列表,列表控件, 可以修改其中的ItemTemplate。
- 3.修改ContentTemplate, 例UserControl控件的數(shù)據(jù)展現(xiàn)形式。
CellTemplate 模板
下面用一個例子, 來演示CellTemplate使用。例子實(shí)現(xiàn)一個DataGrid 展示一個普通的數(shù)據(jù)標(biāo), 同時新增一列CellTemplate添加兩個自定義的按鈕, 如下圖所示。
<DataGrid Name="gd" AutoGenerateColumns="False" CanUserSortColumns="True" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding UserName}" Width="100" Header="學(xué)生姓名"/>
<DataGridTextColumn Binding="{Binding ClassName}" Width="100" Header="班級名稱"/>
<DataGridTextColumn Binding="{Binding Address}" Width="200" Header="地址"/>
<DataGridTemplateColumn Header="操作" Width="100" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left">
<Button Content="編輯"/>
<Button Margin="8 0 0 0" Content="刪除" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>完成操作, 然后后臺進(jìn)行該DataGrid進(jìn)行綁定數(shù)據(jù), 查詢綁定后的效果。
List<Student> students = new List<Student>();
students.Add(new Student() { UserName = "小王", ClassName = "高二三班", Address = "廣州市" });
students.Add(new Student() { UserName = "小李", ClassName = "高三六班", Address = "清遠(yuǎn)市" });
students.Add(new Student() { UserName = "小張", ClassName = "高一一班", Address = "深圳市" });
students.Add(new Student() { UserName = "小黑", ClassName = "高一三班", Address = "贛州市" });
gd.ItemsSource = students;最終的效果, 在數(shù)據(jù)的表格最后一列, 將會在一列中分別生成 兩個普通按鈕。

ItemTemplate
在列表的控件中, 常常會出現(xiàn)一些需求, 類似在下拉控件或樹控件中添加一個 CheckBox選擇框, 一個圖標(biāo)或圖片, 這個時候, 我們就可以利用自定義的DataTemplate 來實(shí)現(xiàn)這個功能,
接下來, 用一個示例來簡單演示其功能, 同樣, 該例子演示利用 ListBox 和 ComboBox來綁定一個 顏色代碼列表, 同時展示其顏色。
<Window.Resources>
<DataTemplate x:Key="comTemplate">
<StackPanel Orientation="Horizontal" Margin="5,0">
<Border Width="10" Height="10" Background="{Binding Code}"/>
<TextBlock Text="{Binding Code}" Margin="5,0"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<ComboBox Name="cob" Width="120" Height="30" ItemTemplate="{StaticResource comTemplate}"/>
<ListBox Name="lib" Width="120" Height="100" Margin="5,0" ItemTemplate="{StaticResource comTemplate}"/>
</StackPanel>
</Grid>上面的代碼中, 定義了一個DataTemplate , 頂一個 長寬10px的border用于顯示顏色代碼, 綁定到Border背景顏色上, 定義了一個TextBlock用于展示顏色的代碼。
下面為后臺的綁定代碼
List<Color> ColorList = new List<Color>();
ColorList.Add(new Color() { Code = "#FF8C00" });
ColorList.Add(new Color() { Code = "#FF7F50" });
ColorList.Add(new Color() { Code = "#FF6EB4" });
ColorList.Add(new Color() { Code = "#FF4500" });
ColorList.Add(new Color() { Code = "#FF3030" });
ColorList.Add(new Color() { Code = "#CD5B45" });
cob.ItemsSource = ColorList;
lib.ItemsSource = ColorList;最終的測試效果如下所示:

ItemsControl
定義ItemsControl 主要分兩個步驟:
- 1.設(shè)置ItemsPanel容器, 用于容納列表的最外層容器
- 2.定義子項(xiàng)的DataTemplate
<ItemsControl Name="ic">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Width="50" Height="50" Content="{Binding Code}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>上面代碼中, 定義了一個WarpPanel 容器為ItemControl的 最外層容器, 子項(xiàng)數(shù)據(jù)模板則綁定了一個按鈕, 后臺代碼綁定幾條數(shù)據(jù), 查看其效果: 橫排排列五個按鈕, 內(nèi)容分別是 1~6.
List<Test> tests = new List<Test>();
tests.Add(new Test() { Code = "1" });
tests.Add(new Test() { Code = "2" });
tests.Add(new Test() { Code = "3" });
tests.Add(new Test() { Code = "4" });
tests.Add(new Test() { Code = "6" });
ic.ItemsSource = tests;
查看ItemsControl可視化樹的結(jié)構(gòu)組成?

剖析該結(jié)構(gòu), 可以看到, 紫色的1處, 為最外層的WrapPanel容器, 用于容納排列按鈕, 由于該示例設(shè)置了 Orientation="Horizontal" , 所以按鈕則按水平排列, 再看 橘色 2處, 可以看見子項(xiàng)外層由一個內(nèi)容呈現(xiàn)包括著, 內(nèi)容為一個按鈕, 由于綁定搞得數(shù)據(jù)是5個, 所以分別生成了內(nèi)容為1~6的5個按鈕。
說明: 那是不是以為則ItemsPanel 放置任何元素都行? 很明顯是不行的。 ItemsPanel的容器需要滿足一個條件, 則是屬于Panel族的元素, 否則會提示以下錯誤:

關(guān)于每種元素的分類可以看關(guān)于控件介紹的文章: http://www.dhdzp.com/article/236066.htm
ContentTemplate
長話短說, 這個東西用的太少了, 詳細(xì)的可以搜索一下相關(guān)的使用資料。
到此這篇關(guān)于WPF中的數(shù)據(jù)模板用法介紹的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#?WPF數(shù)據(jù)綁定模板化操作的完整步驟
- WPF使用ValidationRules對MVVM架構(gòu)數(shù)據(jù)驗(yàn)證
- WPF實(shí)現(xiàn)數(shù)據(jù)綁定
- WPF PasswordBox進(jìn)行數(shù)據(jù)綁定方法
- C# WPF如何反射加載Geometry幾何圖形數(shù)據(jù)圖標(biāo)
- WPF簡單的數(shù)據(jù)庫查詢實(shí)例
- C#中WPF ListView綁定數(shù)據(jù)的實(shí)例詳解
- wpf將表中數(shù)據(jù)顯示到datagrid示例
- 解析WPF綁定層次結(jié)構(gòu)數(shù)據(jù)的應(yīng)用詳解
- WPF的數(shù)據(jù)綁定詳細(xì)介紹
相關(guān)文章
.Net行為型設(shè)計(jì)模式之策略模式(Stragety)
這篇文章介紹了.Net行為型設(shè)計(jì)模式之策略模式(Stragety),文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
ASP.NET web.config 配置節(jié)點(diǎn)詳解
這篇文章主要介紹了ASP.NET web.config 節(jié)點(diǎn)的配置,講解的非常詳細(xì),需要的朋友可以參考下。2016-06-06
ASP.Net?Core?MVC基礎(chǔ)系列之獲取配置信息
這篇文章介紹了ASP.Net?Core?MVC獲取配置信息的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
asp.net下SQLite(輕量級最佳數(shù)據(jù)庫) 原理分析和開發(fā)應(yīng)用
SQLite是一個開源的嵌入式關(guān)系數(shù)據(jù)庫,它在2000年由D. Richard Hipp發(fā)布,它的減少應(yīng)用程序管理數(shù)據(jù)的開銷,SQLite可移植性好,很容易使用,很小,高效而且可靠2011-10-10

