简体   繁体   English

WPF DataGridCheckBoxColumn的更改行为null否

[英]WPF Change behavior of DataGridCheckBoxColumn null false

I have DataGrid and List of objects. 我有DataGrid和对象列表。 DataGrid is only to visualization. DataGrid仅用于可视化。 Now I want to change behavior of binding to DataGridCheckBoxColumn. 现在,我想更改绑定到DataGridCheckBoxColumn的行为。 I want three states like that: 我想要这样的三个状态:

null = unchecked
false = half checked
true = checked

Right now it looks like that: 现在看起来像这样:

null = half checked
false = unchecked
true = checked

I can change the logic inside code and treat null as false and false as null but for me better solution will be just different displaying. 我可以更改代码内部的逻辑,并将null视为false并将false视为null,但对我来说,更好的解决方案将是不同的显示。 Binding looks like that 绑定看起来像这样

<DataGridCheckBoxColumn Header="SomeColumn" Binding="{Binding SomeProperty}" x:Name="SomeName" Visibility="Visible"/>

You could simply use a Converter like so: 您可以像这样简单地使用Converter:

public class CheckBoxConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return false;
        if ((bool) value)
            return true;
        return null;   //value is false
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Add the convert back if needed
        throw new NotImplementedException();
    }
}

and the Xaml will be : Xaml将是:

 <DataGridCheckBoxColumn Header="SomeColumn" Binding="{Binding SomeProperty,Converter={StaticResource CheckBoxConverter}}" x:Name="SomeName" Visibility="Visible"/>

and don't forget to add the Converter to your window (or page) resources : 并且不要忘记将Converter添加到您的窗口(或页面)资源中:

 <Window.Resources>
    <converters:CheckBoxConverter x:Key="CheckBoxConverter"/>
</Window.Resources>

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

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