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