Define DataGrid rows declaratively in XAML

I have this code, and I need to bind the second and third cells in the rows to various properties. Sorry for my bad english.

<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Name="measureDataGrid" VerticalAlignment="Stretch">
                                        <DataGrid.Columns>
                                            <DataGridTextColumn x:Name="measureName" Header="" Width="*" />
                                            <DataGridTextColumn x:Name="measureValue" Header="" Width="Auto" />
                                            <DataGridTextColumn x:Name="measureDestValue" Header=" " Width="Auto" />
                                        </DataGrid.Columns>
                                        <DataGrid.Items>
                                            <!--<DataGridRow>-->
                                            <RowDefinition/>
                                            <RowDefinition/>
                                            <RowDefinition/>                                                           
                                        </DataGrid.Items>                                   
                                    </DataGrid>
+3
source share
1 answer

So, if you need to convert a value (this can be used to display different values ​​in the same column for different rows), you can use ValueConverter.

Create a class that implements IValueConverter. It looks something like this:

public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
{
   bool boolValue = (bool)value;

   if(boolValue)
    return x;
   else
    return y;

}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    throw new NotImplementedException();
}

Create a link to it in your xaml:

<converters: MeasureConverter x:Key="MeasureConverter" />

And use in your DataGridTextColumn, like this:

<DataGridTextColumn Header="measureRow1" DataMemberBinding="{Binding ValueThatWouldDetermineWhatToShow, Converter={StaticResource MeasureConverter}}" />
<DataGridTextColumn Header="measureRow2" DataMemberBinding="{Binding ValueThatWouldDetermineWhatToShow, Converter={StaticResource MeasureConverter}}" />
0
source

Source: https://habr.com/ru/post/1769323/


All Articles