WPF使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate
起因
我們都知道, 在XAML界面當(dāng)中編寫DataTemplate很簡(jiǎn)單, 但是有時(shí)候我們需要在代碼當(dāng)中去設(shè)置DataTemplate。
該怎么辦?
比如, 實(shí)際需求是DataGrid當(dāng)中需要?jiǎng)?chuàng)建100個(gè)DataTemplate列, 很明顯,這些列不太方便在XAML中編寫。
這個(gè)時(shí)候,我們就需要在代碼當(dāng)中動(dòng)態(tài)生成模板列。
答案
如下面所示,我創(chuàng)建了一個(gè)DataGridTemplateColumn,其中包含了一個(gè)StackPanel里面放了兩個(gè)Button按鈕。
<DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="編輯"/> <Button Content="刪除"/> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>
現(xiàn)在就是, 我們需要把這個(gè)過程用代碼去生成, 這個(gè)時(shí)候我們就可以用到FrameworkElementFactory 類。
步驟分為幾步:
- 創(chuàng)建DataGridTemplateColumn 對(duì)象, 設(shè)置Header等內(nèi)容
DataGridTemplateColumn templateColumn = new DataGridTemplateColumn(); templateColumn.Header = "標(biāo)題";
- 創(chuàng)建 FrameworkElementFactory 對(duì)象, 設(shè)置Orientation屬性水平排列
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(StackPanel)); factory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
- 向 FrameworkElementFactory 對(duì)象追加一個(gè)factory對(duì)象
FrameworkElementFactory buttonEdit = new FrameworkElementFactory(typeof(Button)); buttonEdit.SetValue(ContentProperty, "編輯"); factory.AppendChild(buttonEdit); FrameworkElementFactory buttonDel = new FrameworkElementFactory(typeof(Button)); buttonDel.SetValue(ContentProperty, "刪除"); factory.AppendChild(buttonDel);
- 創(chuàng)建DataTemplate對(duì)象, 設(shè)置VisualTree 值為factory
DataTemplate dataTemplate = new DataTemplate(); dataTemplate.VisualTree = factory;
- 最后把DataGridTemplateColumn 的CellTemplate 值設(shè)置為dataTemplate
templateColumn.CellTemplate = dataTemplate;
最終效果
關(guān)于整個(gè)過程梳理
有一點(diǎn),我們需要清楚, 在XAML界面當(dāng)中編寫的任何代碼, 其實(shí)本質(zhì)上都是轉(zhuǎn)化成C#代碼, 既然如此來說, 只要XAML有的對(duì)象,我們都可以用C#代碼編寫, 但是為什么一般我們不這么做, 是因?yàn)閄AML更加容易去表達(dá)界面上的元素, 代碼的可視化以及可維護(hù)性。
再回到上面, 我們需要清楚上面的流程, 我們通過FrameworkElementFactory 創(chuàng)建了一個(gè)完整的視覺樹的對(duì)象,里面包含了一個(gè)StackPanel容器,容器中放置了兩個(gè)Button控件,最終把這個(gè) FrameworkElementFactory 對(duì)象給了DataTemplate當(dāng)中的VisualTree, 這里的意思是 我們給DataTemplate設(shè)置了可視化的視覺樹結(jié)構(gòu), 最終DataTemplate決定了 DataGridTemplateColumn 的視覺呈現(xiàn)。
完整代碼
DataGridTemplateColumn CreateDataGridTemplateColumn() { DataGridTemplateColumn templateColumn = new DataGridTemplateColumn(); templateColumn.Header = "標(biāo)題"; FrameworkElementFactory factory = new FrameworkElementFactory(typeof(StackPanel)); factory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); FrameworkElementFactory buttonEdit = new FrameworkElementFactory(typeof(Button)); buttonEdit.SetValue(ContentProperty, "編輯"); factory.AppendChild(buttonEdit); FrameworkElementFactory buttonDel = new FrameworkElementFactory(typeof(Button)); buttonDel.SetValue(ContentProperty, "刪除"); factory.AppendChild(buttonDel); DataTemplate dataTemplate = new DataTemplate(); dataTemplate.VisualTree = factory; templateColumn.CellTemplate = dataTemplate; return templateColumn; }
到此這篇關(guān)于WPF使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net core配合vue實(shí)現(xiàn)后端驗(yàn)證碼邏輯
網(wǎng)上的前端驗(yàn)證碼邏輯總感覺不安全,驗(yàn)證碼建議還是使用后端配合驗(yàn)證。本文主要介紹了asp.net core配合vue實(shí)現(xiàn)后端驗(yàn)證碼邏輯,感興趣的可以了解一下2021-06-06ubuntu16.4下用jexus部署ASP.NET Core環(huán)境
這篇文章主要以圖文結(jié)合的方式介紹了ubuntu16.4下ASP.NET Core部署環(huán)境搭建步驟,感興趣的小伙伴們可以參考一下2016-07-07FileUpload1 上傳文件類型驗(yàn)證正則表達(dá)式
FileUpload1 上傳文件類型驗(yàn)證正則表達(dá)式...2006-10-10詳解Asp.net web.config customErrors 如何設(shè)置
這篇文章主要介紹了詳解Asp.net web.config customErrors 如何設(shè)置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02使用.NET Core實(shí)現(xiàn)餓了嗎拆紅包功能
這篇文章主要介紹了使用.NET Core實(shí)現(xiàn)餓了嗎拆紅包功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07asp.net提取多層嵌套json數(shù)據(jù)的方法
這篇文章主要介紹了asp.net提取多層嵌套json數(shù)據(jù)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了asp.net解析json格式數(shù)據(jù)的步驟與相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06深入理解__doPostBack 客戶端調(diào)用服務(wù)端事件
__doPostBack是一個(gè)純粹并且是非常簡(jiǎn)單的javascript函數(shù),大部分的頁面PostBack都是由它觸發(fā)的。2008-08-08ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡(jiǎn)易的方法
ASP.NET技巧:數(shù)據(jù)島出到Excel最為簡(jiǎn)易的方法...2006-09-09.net 通過URL推送POST數(shù)據(jù)具體實(shí)現(xiàn)
這篇文章主要介紹了.net 通過URL推送POST數(shù)據(jù)具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-12-12c# Random快速連續(xù)產(chǎn)生相同隨機(jī)數(shù)的解決方案
在寫數(shù)獨(dú)基類的時(shí)候?yàn)榱水a(chǎn)生隨機(jī)數(shù)的時(shí)候遇到奇怪的問題2009-03-03