以编程方式为 DataGrid 创建 WPF DataGridTemplateColumn
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/613158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically create WPF DataGridTemplateColumn for DataGrid
提问by PaulWaldman
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?
我希望能够根据我的数据源以编程方式创建 DataGridTemplateColumns。例如,如果我的源在特定列中有一个日期,我希望能够使用 Datepicker 控件。我知道这可以在设计时使用 xaml 和 DataGridTemplateColumn 轻松完成,但是,我将如何在运行时完成此操作?
Is my best option xamlreader.load or a more traditional route like:
我最好的选择是 xamlreader.load 还是更传统的路线,例如:
Dim TempCol As Microsoft.Windows.Controls.DataGridTemplateColumn
Dim TempCol As Microsoft.Windows.Controls.DataGridTemplateColumn
I have not had any success with the latter.
我在后者方面没有取得任何成功。
Thanks.
谢谢。
-Paul
-保罗
Edit: This is the code I attempted to use:
编辑:这是我尝试使用的代码:
Dim TempCol As New Microsoft.Windows.Controls.DataGridTemplateColumn
TempCol.CellEditingTemplate = DataTemplate.Equals(DatePicker)
I receive DatePicker is a type and cannot be used as an expression.
我收到 DatePicker 是一种类型,不能用作表达式。
I am basiing this on the WPF Toolkit demo. http://windowsclient.net/wpf/wpf35/wpf-35sp1-toolkit-datagrid-feature-walkthrough.aspx
我基于 WPF Toolkit 演示。 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>
Thanks!
谢谢!
回答by Andy
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.
您的代码不起作用的原因是因为您将CellEditingTemplate
列的值设置为 a bool
(调用的结果DataTemplate.Equals()
,而不是在代码中创建模板的实例。
You can create a template in code using something like this (equivalent to the XAML code snippet you provided):
您可以使用这样的代码(相当于您提供的 XAML 代码片段)在代码中创建模板:
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;
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?
我不确定这种方法是否比自己加载 XAML 更好。也许尝试两种方法,看看哪种方法最适合您,并且效果更快?