我有这个代码,我需要将行中的第二个和第三个单元格绑定到不同的属性。抱歉我的英语不好。
<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>
答案 0 :(得分:0)
因此,如果您需要转换一个值(这可以用于在不同行的同一列中显示不同的值),您可以使用ValueConverter。
创建一个实现IValueConverter的类看起来像这样:
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();
}
在xaml中创建对它的引用:
<converters: MeasureConverter x:Key="MeasureConverter" />
并在您的DataGridTextColumn中使用,如下所示:
<DataGridTextColumn Header="measureRow1" DataMemberBinding="{Binding ValueThatWouldDetermineWhatToShow, Converter={StaticResource MeasureConverter}}" />
<DataGridTextColumn Header="measureRow2" DataMemberBinding="{Binding ValueThatWouldDetermineWhatToShow, Converter={StaticResource MeasureConverter}}" />