溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2022-02-11 14:37:52 來(lái)源:億速云 閱讀:190 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“WPF怎么使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“WPF怎么使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate”吧!

起因

我們都知道, 在XAML界面當(dāng)中編寫(xiě)DataTemplate很簡(jiǎn)單, 但是有時(shí)候我們需要在代碼當(dāng)中去設(shè)置DataTemplate。

該怎么辦?

比如, 實(shí)際需求是DataGrid當(dāng)中需要?jiǎng)?chuàng)建100個(gè)DataTemplate列, 很明顯,這些列不太方便在XAML中編寫(xiě)。

這個(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è)過(guò)程用代碼去生成, 這個(gè)時(shí)候我們就可以用到FrameworkElementFactory 類(lèi)。

步驟分為幾步:

  • 創(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;

最終效果

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

關(guān)于整個(gè)過(guò)程梳理

有一點(diǎn),我們需要清楚, 在XAML界面當(dāng)中編寫(xiě)的任何代碼, 其實(shí)本質(zhì)上都是轉(zhuǎn)化成C#代碼, 既然如此來(lái)說(shuō), 只要XAML有的對(duì)象,我們都可以用C#代碼編寫(xiě), 但是為什么一般我們不這么做, 是因?yàn)閄AML更加容易去表達(dá)界面上的元素, 代碼的可視化以及可維護(hù)性。

再回到上面, 我們需要清楚上面的流程, 我們通過(guò)FrameworkElementFactory 創(chuàng)建了一個(gè)完整的視覺(jué)樹(shù)的對(duì)象,里面包含了一個(gè)StackPanel容器,容器中放置了兩個(gè)Button控件,最終把這個(gè) FrameworkElementFactory 對(duì)象給了DataTemplate當(dāng)中的VisualTree, 這里的意思是 我們給DataTemplate設(shè)置了可視化的視覺(jué)樹(shù)結(jié)構(gòu), 最終DataTemplate決定了 DataGridTemplateColumn 的視覺(jué)呈現(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;
        }

感謝各位的閱讀,以上就是“WPF怎么使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)WPF怎么使用代碼創(chuàng)建數(shù)據(jù)模板DataTemplate這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

wpf
AI