2014-12-09 30 views
0

我有DataGrid和对象列表。 DataGrid仅用于可视化。现在我想更改绑定到DataGridCheckBoxColumn的行为。我想三个州这样的:WPF更改DataGridCheckBoxColumn的行为null false

null = unchecked 
false = half checked 
true = checked 

现在它看起来像这样:

null = half checked 
false = unchecked 
true = checked 

我可以改变的逻辑里面的代码和治疗无效的虚假和错误的为空,但对我来说更好的解决方案只是不同的展示。 绑定看起来像

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

回答

0

你可以简单地用一个转换器,像这样:

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(); 
    } 
} 

和XAML中会:

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

,不要忘记添加转换器到您的窗口(或页面)的资源:

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

谢谢,这项工作完全按照我的意愿 – Marcin1199 2014-12-09 15:13:12

相关问题