为DataGrid编程创建WPF DataGridTemplateColumn [英] Programmatically create WPF DataGridTemplateColumn for DataGrid

查看:172
本文介绍了为DataGrid编程创建WPF DataGridTemplateColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够以编程方式根据我的数据源创建DataGridTemplateColumns。例如,如果我的源在特定列中有日期,我希望能够使用Datepicker控件。我知道在设计时,这可以很容易地与xaml和DataGridTemplateColumn完成,但是,如何在运行时完成这个?

I would like to be able to programmatically create DataGridTemplateColumns based on my data source. For example, if my source has a date in a particular column I would like to be able to utilize a Datepicker control. I know this is easily accomplished with xaml and a DataGridTemplateColumn at design-time, however, how would I accomplish this at run-time?

是我最好的选择xamlreader.load或更传统的路线,如:

Is my best option xamlreader.load or a more traditional route like:

Dim TempCol As Microsoft.Windows.Controls.DataGridTemplateColumn

我没有成功与后者。

谢谢。

-Paul

编辑:
这是我试图使用的代码:

This is the code I attempted to use:

        Dim TempCol As New Microsoft.Windows.Controls.DataGridTemplateColumn

    TempCol.CellEditingTemplate = DataTemplate.Equals(DatePicker)

我收到DatePicker是一种类型,不能用作表达式。

I receive DatePicker is a type and cannot be used as an expression.

我基于这个WPF工具包演示。
http://windowsclient.net/ wpf / wpf35 / wpf-35sp1-toolkit-datagrid-feature-walkthrough.aspx

I am basiing this on the WPF Toolkit demo. http://windowsclient.net/wpf/wpf35/wpf-35sp1-toolkit-datagrid-feature-walkthrough.aspx

<dg:DataGridTemplateColumn Header="Date" MinWidth="100">
    <dg:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <dg:DatePicker SelectedDate="{Binding Date}" SelectedDateFormat="Short" />
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellEditingTemplate>
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Date, StringFormat=d}" />
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

谢谢!

推荐答案

您的代码不起作用的原因是因为您将 CellEditingTemplate 列的值设置为 bool (调用 DataTemplate.Equals()的结果,而不是在代码中创建模板的实例。

The reason that your code does not work is because you are setting the value of the CellEditingTemplate column to a bool (the result of calling DataTemplate.Equals(), rather than creating an instance of the template in code.

您可以使用这样的代码创建一个模板(相当于您提供的XAML代码片段):

You can create a template in code using something like this (equivalent to the XAML code snippet you provided):

DataGridTemplateColumn col = new DataGridTemplateColumn();
col.Header = "Date";

// Create a factory. This will create the controls in each cell of this
// column as needed.
FrameworkElementFactory factory =
    new FrameworkElementFactory(typeof(DatePicker));

// Bind the value of this cell to the value of the Date property of the
// DataContext of this row. The StringFormat "d" will be used to display
// the value.
Binding b = new Binding("Date");
b.StringFormat = "d";
factory.SetValue(DatePicker.SelectedDateProperty, b);

// Create the template itself, and add the factory to it.
DataTemplate cellEditingTemplate = new DataTemplate();
cellEditingTemplate.VisualTree = factory;

col.CellEditingTemplate = cellEditingTemplate;

我不知道这种方法会比自己装载XAML更好。也许尝试这两种方法,看看哪一种最适合你,并且工作更快?

I'm not sure if this approach would work better than loading the XAML yourself. Maybe try both approaches and see which one works best for you, and works faster?

这篇关于为DataGrid编程创建WPF DataGridTemplateColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆