【问题标题】:Change autogenerated column type in WPF datagrid在 WPF 数据网格中更改自动生成的列类型
【发布时间】:2016-08-26 14:48:44
【问题描述】:

基础数据具有 Char 类型和值“Y”、“N”。如何在保留绑定的同时使网格显示复选框列?我试过了

private void grdScenarioList_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (e.PropertyType == typeof(System.Char))
        {
            // Create a new column.
            DataGridCheckBoxColumn column = new DataGridCheckBoxColumn();
            column.Header = e.Column.Header;
            //column.Binding = (e.Column as DataGridCheckBoxColumn).Binding; 

            // Replace the auto-generated column with the templateColumn.
            e.Column = column;
        }

它显示复选框列但绑定丢失。 如果我取消注释 column.binding 行,我会收到一条错误消息,指出 (e.Column as DataGridCheckBoxColumn).Binding 为空。有办法吗? 我有一个 CharToBooleanConverter 类,用于其他地方的复选框,但我不知道如何在这里分配它。

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    找到解决办法

    if (e.PropertyType == typeof(System.Char))
            {
                DataGridCheckBoxColumn col = new DataGridCheckBoxColumn();
                col.Header = e.Column.Header;
                Binding binding = new Binding(e.PropertyName);
                binding.Converter = new CharToBooleanConverter();
                col.Binding = binding;
                e.Column = col;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-02-20
      • 2018-02-24
      相关资源
      最近更新 更多