编程创建WPF DataGridTemplateColumn的数据网格 [英] Programmatically create WPF DataGridTemplateColumn for DataGrid
问题描述
我希望能根据我的数据源以编程方式创建DataGridTemplateColumns。例如,如果我的源代码中有一个日期在一个特定的专栏中,我希望能够利用一个日期选择器控制。我知道这是很容易做到使用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:
昏暗TempCol作为Microsoft.Windows.Controls.DataGridTemplateColumn
我还没有与后者没有成功。
I have not had any success with the latter.
感谢。
- 保罗
编辑: 这是code我尝试使用:
This is the code I attempted to use:
Dim TempCol As New Microsoft.Windows.Controls.DataGridTemplateColumn
TempCol.CellEditingTemplate = DataTemplate.Equals(DatePicker)
予接收的DatePicker是一种类型的,并且不能被用作前pression
I receive DatePicker is a type and cannot be used as an expression.
我在WPF工具包演示basiing这一点。 的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>
谢谢!
推荐答案
这是你的code不工作的原因是因为你设置 CellEditingTemplate
列调用的布尔
(结果 DataTemplate.Equals()
,而不是创建模板的一个实例在code。
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 code)创建于code模板
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?
这篇关于编程创建WPF DataGridTemplateColumn的数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!