Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 601
Hugo Avatar asked Oct 28 '25 14:10

Hugo


1 Answers

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>
like image 120
ASh Avatar answered Oct 31 '25 04:10

ASh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!