框架元素工厂信息
本文关键字:信息 工厂 元素 框架 | 更新日期: 2023-09-27 18:33:20
我一直在互联网上到处寻找有关FrameworkElementFactory
类的适当文档,但我似乎找不到合适的教程或有用的信息。
对这个主题有更多了解的人可以给我更多关于它的信息吗?以下是我到目前为止从这个问题中发现的内容:(感谢鲍勃(
将项目源绑定到myDataGrid
:
Binding dataGridItemsSourceBinding = new Binding("MyItemsSourceName");
myDataGrid.SetBinding(DataGrid.ItemsSourceProperty, datagridItemsSourceBinding);
创建数据网格模板列
DataGridTemplateColumn templatecolumn = new DataGridTemplateColumn() {
Header = "myColumnName", // Add the name of your column here
};
创建数据模板,以便在数据网格列的数据单元格中显示值
// 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;
创建数据模板,以便在编辑数据网格列的数据单元格中的值
// 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;
将完成的数据网格列添加到数据网格
// Add the completed DataGridColumn to your DataGrid
myDataGrid.Columns.Add(templateColumn);
请参考这个线程:使用FrameworkElementFactory的目的
至于现在,Microsoft不建议使用FrameworkElementFactory