亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2022年02月11日 11:54:11   作者:痕跡g  
本文詳細(xì)講解了WPF使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

起因

我們都知道, 在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)文章

最新評(píng)論