简体   繁体   English

当AutoGenerateColumns为可空的bool时,WPF DataGrid强制绑定DataGridCheckBoxColumn

[英]WPF DataGrid force binding DataGridCheckBoxColumn when AutoGenerateColumns for nullable bool

We have lots of DataGrid with dynamic data binding, so we always use AutoGenerateColumns=True . 我们有很多带动态数据绑定的DataGrid ,所以我们总是使用AutoGenerateColumns=True For bool columns generates a DataGridCheckBoxColumn but for a nullable bool ( bool? in C#) generates the default DataGridTextColumn . 对于bool列,生成一个DataGridCheckBoxColumn但是对于可以为空的bool(在C#中bool? )生成默认的DataGridTextColumn Is there any way to force the DataGrid generate automatically a DataGridCheckBoxColumn for nullable bool? 有没有办法强制DataGrid自动生成一个可空的bool的DataGridCheckBoxColumn Prefer not implementation-dependant hacks. 不喜欢依赖于实现的黑客攻击。 Also some code-behind is accepted for example in the AutoGeneratingColumn event. 例如,在AutoGeneratingColumn事件中也接受了一些代码隐藏。

you should register to AutoGeneratingColumn event and change the generated column based on column type, like so: 您应该注册到AutoGeneratingColumn事件并根据列类型更改生成的列,如下所示:

  private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (e.PropertyType == typeof(bool?))
        {
             DataGridCheckBoxColumn  checkBoxColumn=new DataGridCheckBoxColumn();
            checkBoxColumn.Header = e.Column.Header;
            checkBoxColumn.Binding = new Binding(e.PropertyName);
            checkBoxColumn.IsThreeState = true;

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

You'd have to try it out - I'm not near VS - but you should be able use this to easily solve it... 你必须尝试一下 - 我不在VS附近 - 但你应该能够用它来轻松解决它......

Look at the other post of mine ... 看看我的另一篇文章 ......
how to stop a specific column to be generated in DataGrid when the AutoGenerateColumns is set to True? 当AutoGenerateColumns设置为True时,如何停止在DataGrid中生成特定列?

Use that 'method' to bind to your command 使用'method' bind到您的命令
(all is MVVM straight - you just need an AutoGenerateColumnEvent attached property. (所有都是MVVM直接 - 您只需要一个AutoGenerateColumnEvent附加属性。

Then in your command handling, like I did there - replace the column - like already suggested. 然后在你的命令处理中,就像我在那里做的那样 - 替换列 - 就像已经建议的那样。

1) Use the Attach class provided, add namespace etc. 1)使用提供的Attach类,添加命名空间等。

2) And in your XAML... 2)在你的XAML ......

<DataGrid 
    local:Attach.AutoGenerateColumnEvent="{Binding AutoGeneratingColumnCommand}" AutoGenerateColumns="True" />  

3) And in your view-model... 3)在你的视图模型中......

RelayCommand _autoGeneratingColumnCommand;
public RelayCommand AutoGeneratingColumnCommand 
{ 
    get 
    { 
        return _autoGeneratingColumnCommand ?? (_autoGeneratingColumnCommand = new RelayCommand(param => 
        { 
            var e = param as DataGridAutoGeneratingColumnEventArgs;
            var type = ((PropertyDescriptor)e.PropertyDescriptor).PropertyType;
            if (type == typeof(bool?))
                e.Column = new DataGridCheckBoxColumn();
        }, 
        param => true)); 
    } 
}

Note: For a more generic solution... 注意:对于更通用的解决方案......

You could use the PropertyDescriptor to get the property type - and make a generic decision based on it - that's pretty much what you need I think, I haven't tried though. 你可以使用PropertyDescriptor来获取属性类型 - 并根据它做出一个通用的决定 - 这几乎是你需要的,我想,我没有尝试过。 So you'd have to try it out. 所以你必须尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM