Hugo
Hugo

Reputation: 163

XAML DataGridTextColumn: display an empty cell when condition is false

I have a Collection of Article and some of them have a price and others don't.

When a price is available I want my datagrid to display it even if it's 0.

When a price is not possible on the article the default value is 0 and I don't want it to appear in the datagrid and the cell should be in readonly.

This is what I tried:

<DataGridTextColumn Header="PU" Binding="{Binding PU, UpdateSourceTrigger=PropertyChanged}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Mesurage.HasPrice}" Value="False">
                    <Setter Property="Focusable" Value="False"/>
                    <Setter Property="TextBlock.Text" Value="{Binding PU, StringFormat=0;;#, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

I used a StringFormat to hide the 0 but it still show it in my grid. Aslo the IsReadOnly property could not be modified in the trigger so I use the Focusable property instead.

Is there a way to achieve the wanted result in XAML only? Thank you.

Upvotes: 4

Views: 1677

Answers (1)

ASh
ASh

Reputation: 35681

try set IsEnabled to false to disable input

also set Foreground to Transparent: the 0 value will be there but not visible

<DataTrigger Binding="{Binding Mesurage.HasPrice}" Value="False">
    <Setter Property="IsEnabled" Value="False"/>
    <Setter Property="Foreground" Value="Transparent"/>
</DataTrigger>

Upvotes: 4

Related Questions