欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate

 更新時間:2022年02月11日 11:54:11   作者:痕跡g  
本文詳細講解了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的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論