开发者

Define DataGrid's rows declaratively in XAML

I have this code and I need to bind second and third cells in rows to different 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>-->
                                            <RowDef开发者_高级运维inition/>
                                            <RowDefinition/>
                                            <RowDefinition/>                                                           
                                        </DataGrid.Items>                                   
                                    </DataGrid>


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

Create a class that implements IValueConverter That would look 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 reference 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜