c# - 网格控件 WPF 的动态列

标签 c# wpf mvvm

我在 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 个带有按钮或图像的新列。所以我想我需要设置单元格模板。

我该怎么做?

最佳答案

如果您想添加一个带有ButtonImageColumn,您需要定义一个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/

相关文章:

c# - 从 C# 中拖动 excel 中的公式

c# - ASP.NET 获取 Web 应用程序的所有用户 session

wpf - 使用 siteoforigin 包 Uri 单元测试 WPF 应用程序

c# - OnPropertyChanged() 与 PropertyChanged 事件?

wpf - invokecommandaction使用可以执行吗?

silverlight - 如何在 silverlight 中使用 LINQ join Query 将两个 ObservableCollection 加入一个 ObservableCollection

c# - 如何在 Xamarin 中将特定数据更新到 SQLite

c# - Unity3D通过Xcode访问变量集

c# - 如何绑定(bind)到 wpf 中另一个对象的属性?

c# - 如何在 WPF 应用程序中使用媒体按键和音量变化?