我在 wpf View 中有一个网格控件,我从一个文件中读取了更多字段,我想在 View 模型中为这些字段生成列(我使用 MVVM)。所以我有一个类似这样的功能:
GridColumn column;
try
{
column = new GridColumn
{
Name = fieldName,
Header = headerName,
AllowSorting = DefaultBoolean.True,
FieldName = fieldName,
ReadOnly = isReadOnly,
AllowEditing = isReadOnly ? DefaultBoolean.False : DefaultBoolean.True,
AllowMoving = DefaultBoolean.True,
AllowResizing = DefaultBoolean.True,
Width = width,
EditSettings = new TextEditSettings { HorizontalContentAlignment = EditSettingsHorizontalAlignment.Center },
AllowDrop = true,
CellStyle = isDiffColumn ? GetDiffColumnStyle(fieldName) : null
};
}
但现在我想在我的网格中添加 3 个带有按钮或图像的新列。所以我想我需要设置单元格模板。
我该怎么做?
最佳答案
如果您想添加一个带有Button
或Image
的Column
,您需要定义一个DataGridTemplateColumn
.
例如:
var templateColumn = new DataGridTemplateColumn();
var template = new DataTemplate();
var button = new Button();
template.VisualTree = button;
templateColumn.CellTemplate = template;
关于c# - 网格控件 WPF 的动态列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30847476/