如何使用C#代码实现DataTemplate

开发 后端
不得不说DataTemplate是WPF中的一项重要技术组成,可以方便让我们实现各种各样的UI样式,大大丰富了应用程序的界面设计。

不得不说DataTemplate是WPF中的一项重要技术组成,可以方便让我们实现各种各样的UI样式,大大丰富了应用程序的界面设计。通常我们都会在XAML文件中定义出具体的DataTemplate, 我举一个ListView的例子:

<ListView IsSynchronizedWithCurrentItem="True" Background="#FFFFFFFF" 

Foreground="White" ItemsSource="{Binding}" Style="{DynamicResource

SuperListViewStyle}" ItemContainerStyleSelector="{StaticResource

listViewItemStyleSelector}" x:Name="lvExecutionTable" >
                        <ListView.View>
                            <GridView>
                                <GridViewColumn>
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox HorizontalAlignment="Center"

IsChecked="{Binding Path=IsSelected}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                    <GridViewColumnHeader Content="选择"

Tag="IsSelected"/>
                                </GridViewColumn>
                                <GridViewColumn>
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding

Path=IsComfirmed, Converter={StaticResource IsComfirmedConverter}}"

Foreground="{Binding Path=IsComfirmed, Converter={StaticResource

IsComfirmedToColorConverter}}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                    <GridViewColumnHeader Content="确认状态"

Tag="IsComfirmed"/>
                                </GridViewColumn>
                                <GridViewColumn DisplayMemberBinding="{Binding

Path=MarketIndicator}">
                                    <GridViewColumnHeader Content="市场类型"

Tag="MarketIndicator"/>
                                </GridViewColumn>
                                <GridViewColumn>
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding

Path=TradeType}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                    <GridViewColumnHeader Content="成交方式"

Tag="TradeType"/>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>

例子中对每个Column定义了简单的DataTemplate。我遇到的问题是如果这个List View需要动态编辑列,或者新加入一些列,就是如何用代码来实现对控件的DataTemplate的操作。经过查阅MSDN的相关文档,最好找到了解决的办法,如下:

GridViewColumn column = new GridViewColumn();
            GridViewColumnHeader h = new GridViewColumnHeader();
            h.Content = "??????";
            h.Tag = "*******";
            column.Header = h;
            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof

(CheckBox));
            Binding binding = new Binding();
            binding.Path = new PropertyPath("MarketIndicator");
            fef.SetBinding(CheckBox.ContentProperty, binding);
            fef.SetValue(CheckBox.ForegroundProperty, Brushes.White);
            dt.VisualTree = fef;
            column.CellTemplate = dt;

            (this.lvExecutionTable.View as GridView).Columns.Add(column);

DataTemplate对象的VisualTree属性的类型是FrameworkElementFactory,用来存放具体的Control。FrameworkElementFactory fef = new FrameworkElementFactory(typeof(CheckBox))语句定义了DataTemplate的顶层元素是一个CheckBox控件,使用fef.SetValue方法可以设置顶层元素的属性,如绑定等等。如果你需要定义的DataTemplate不止一个层次,那么你就要花点时间去设置顶层元素的Content属性。

【编辑推荐】

  1. 浅析C#3.0编码习惯与命名规则
  2. 详解C# 4.0中必选参数与可选参数混合的问题
  3. 深入解析C#编程中的方法重载
责任编辑:彭凡 来源: 博客园
相关推荐

2009-08-18 13:35:06

C#枚举文件

2009-09-02 17:24:44

C#关机代码

2009-08-26 15:09:57

C# Hook

2009-08-19 14:29:33

C#代理

2015-09-16 15:11:58

C#异步编程

2009-08-06 16:44:03

C#创建WebServ

2009-08-19 16:42:41

C#如何使用XML

2009-08-12 18:29:06

C#读取TXT文件

2009-09-10 17:48:05

C# button

2009-06-16 10:20:05

多继承C#

2009-09-01 18:06:06

c#保存窗体状态

2020-12-31 07:31:10

C# 反射数据

2021-01-20 05:53:25

C# ValueTupleTuple

2011-05-18 17:56:38

C#C++

2011-05-18 18:05:47

C#C++

2009-09-08 22:53:39

c# textbox数

2009-09-10 12:00:09

C# listbox

2009-08-19 17:10:09

C#回调函数

2021-02-01 12:36:59

C# Channels存储

2020-05-22 07:00:00

C#用户注释编程语言
点赞
收藏

51CTO技术栈公众号