0
votes

I have a DataGrid that should display values from a datatable object.

I need it to display one row for each in the datatable, but if column ShowRow in the DataTable is set to False, I need all but the first 2 column to display nothing. Right now I am achieving this by binding to a copy of the table and in that copy I manually set the values to empty when needed.

Is there a more elegant way to do this through binding?

2

2 Answers

0
votes

you need to create your custom IValueConverter and for values that should be hidden convert to empty string

0
votes

Yes, you can do it through data triggers.

Example:

  • You create a custom DataGridCell style called MyCellStyle.
  • Whenever the property MyProperty contained in the specific model representing the row is set to True the ForeGround text will be set to Transparent.
  • All you need to do is utilize this on any DataGridCell that you wish to exhibit the desired behavior.

The sample:

<Style x:Key="MyCellStyle" TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <!-- Hide text if MyProperty is "True" -->
        <DataTrigger Binding="{Binding Path=MyProperty, Mode=OneWay}" Value="True">
            <Setter Property="Foreground" Value="Transparent" />            
        </DataTrigger>
    </Style.Triggers>
</Style>

There are of course other options for hiding the text. I would strongly recommend not manipulating the model itself. Affect the content visibility instead since that is the desired behavior.