WPF DataGridCheckBoxColumn: how to hide the checkbox if the binding value is null?
I have a datagrid with a DataGridCheckBoxColumn binding to nullable bool. I'd like to hide the checkbox completely if the value is null. I tried the following trigger, but it doesn't work:
<Style TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter Property="Visibility" Value="Hidden"/>
</Trigger>
</Style.Triggers>
</Style>
Is it p开发者_JAVA技巧ossible at all? Your help is much appreciated!
There are always two styles in a DataGrid, the ElementStyle
and the EditingElementStyle
, your style should be applied as ElementStyle
, then you can still edit the checkbox but it won't be visible when not in edit mode if null. Also the three states need to be enabled.
<DataGridCheckBoxColumn Binding="{Binding MyNullableBool}" IsThreeState="True">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter Property="Visibility" Value="Hidden"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
精彩评论