c# - FrameworkElementFactory 信息

标签 c# wpf datagrid frameworkelementfactory

我一直在 Internet 上到处寻找关于 FrameworkElementFactory 类的适当文档,但我似乎找不到有关它的适当教程或有用信息。

请问对这个问题了解更多的人可以给我更多的信息吗?这是我到目前为止从 THIS 问题中发现的内容:(感谢 Bob)

将 ItemsSource 绑定(bind)到 myDataGrid:

Binding dataGridItemsSourceBinding = new Binding("MyItemsSourceName");
myDataGrid.SetBinding(DataGrid.ItemsSourceProperty, datagridItemsSourceBinding);


创建一个 DataGridTemplateColumn

 DataGridTemplateColumn templatecolumn = new DataGridTemplateColumn() {
      Header = "myColumnName", // Add the name of your column here
 };


当您在 DataGrid 列的 DataCell 中显示值时创建数据模板

 // Displaying Template for when you display the DataCell in the DataGridColumn
 // Create a Data Template for when you are displaying a DataGridColumn
 DataTemplate textBlockTemplate = new DataTemplate();
 // Create a Framework Element for the DataGridColumn type (In this case, a TextBlock)
 FrameworkElementFactory textBlockElement = new FrameworkElementFactory(typeof(TextBlock));
 // Create a Binding to the value being displayed in the DataGridColumn
 Binding textBlockBinding = new Binding("myPropertyName");
 // Assign the Binding to the Text Property of the TextBlock
 textBlockElement.SetBinding(TextBlock.TextProperty, textBlockBinding);
 // Set the DataGridColumn to stretch to fit the text
 textBlockElement.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
 // Add the TextBlock element to the Visual Tree of the Data Template
 textBlockTemplate.VisualTree = textBlockElement;
 // Add the Data Template to the DataGridColumn Cell Template
 templatecolumn.CellTemplate = textBlockTemplate;


为编辑 DataGrid 列的 DataCell 中的值创建数据模板

 // Editing Template for when you edit the DataCell in the DataGridColumn
 // Create a Data Template for when you are displaying a DataGridColumn
 DataTemplate textBoxTemplate = new DataTemplate();
 // Create a Framework Element for the DataGrid Column type (In this case, TextBox so the user can type)
 FrameworkElementFactory textBoxElement = new FrameworkElementFactory(typeof(TextBox));
 // Create a Binding to the value being edited in the DataGridColumn
 Binding textBoxBinding = new Binding("myPropertyName");
 // Assign the Binding to the Text Property of the TextBox
 textBoxElement.SetBinding(TextBox.TextProperty, textBoxBinding);
 // Set the DataGridColumn to stretch to fit the text
 textBlockElement.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
 // Add the TextBox element to the Visual Tree of the Data Template
 textBoxTemplate.VisualTree = textBoxElement;
 // Add the Data Template to the DataGridColumn Cell Editing Template
 templatecolumn.CellEditingTemplate = textBoxTemplate;


将完成的 DataGridColumn 添加到您的 DataGrid

 // Add the completed DataGridColumn to your DataGrid
 myDataGrid.Columns.Add(templateColumn);

最佳答案

请引用此主题:Purpose of using FrameworkElementFactory

目前微软不推荐使用FrameworkElementFactory

关于c# - FrameworkElementFactory 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35209273/

相关文章:

c# - 使用 Viewbox 缩放包含标签和文本框的网格

c# - EF4 代码首先与外部实体建立多对多关系

c# - 使用 linq 生成动态集合

c# - WPF 数据网格中的内存泄漏问题

c# - 绑定(bind)到属性的 WPF Datagrid RowDetailsTemplate 可见性

c# - 默认情况下停止 Datagrid 选择第一行

c# - 是否有 Z.EntityFramework.Extensions 的非商业替代品?

c# - 如何在 Mahapps Metro 输入框中指定插入符位置?

c# - 有没有办法将可观察集合转换为常规集合?

wpf - 相对源绑定(bind)表达式错误