newman
newman

Reputation: 6911

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 possible at all? Your help is much appreciated!

Upvotes: 2

Views: 5036

Answers (1)

brunnerh
brunnerh

Reputation: 185410

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>

Upvotes: 3

Related Questions