Search code examples
c#wpfdatagriddatagridcell

Empty the cell when it is disabled


I have a datagrid which uses Styles of DataGridCell type to disable some cells depending on value from another cell of same row.

Below is the XAML for Style

    <Style x:Key="testCellStyle" TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Segment}" Value="0">
                <Setter Property="IsEnabled" Value="False"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Segment}" Value="1">
                <Setter Property="IsEnabled" Value="True"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

This part is working as intended.

Now, as the columns are bound to VM properties which update the database accordingly, I need to empty the cell before disabling it so as to not to persist junk values in the DB.

Being new to C#, I'd prefer to use XAML for this purpose if possible.

How can this behavior be achieved?


Solution

  • Being new to C#, I'd prefer to use XAML for this purpose if possible.

    XAML is a markup language. It cannot be used to clear any values from your source object.

    What you should do is to clear the values in your data object as soon as the Segment property is set to 1 and you should do this programmatically, either in the class where Segment and the other properties are defined itself, or from the class that sets the Segment property.

    You neither can't nor should clear any values of the underlying data object using a DataTrigger.