下面的WPF代码的注释部分描述了该问题。
我基本上想要绑定到单元格绑定的PropertyName
(非值)。
我想使用Handle
,Price
,Quantity
而不是HandleHeader
,PriceHeader
的属性名绑定,而不是将列名发送到我的转换器。 }和QuantityHeader
。现在,我的转换器获得了HandleHeader
而不是header
。如果我的列名改变了我的代码,那就不行了。
这就是为什么我想使用属性名称,所以如果有人更改了网格列名称,我的dirtyConvertor
仍然有用
<Window x:Class="dataGridColorsWithEvents.GridColorwithConvertor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:y="clr-namespace:dataGridColorsWithEvents.Converters"
Title="GridColorwithConvertor" Height="300" Width="300">
<Window.Resources>
<Style x:Key="DirtyStatus" TargetType="DataGridCell">
<Style.Resources>
<y:isDirtyConverter x:Key="isDirtyConverter" />
</Style.Resources>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource isDirtyConverter}">
<Binding Path="Dirty"></Binding>
<Binding RelativeSource="{RelativeSource Self}" Path="Column.Header"></Binding>
<!-- here instead of binding by the column name, i want | to bind with the property name(Handle,Price,Quantity instead of HandleHeader,PriceHeader and QuantityHeader
|_______________
|
|
\ /
\/
For Eg. <DataGridTextColumn Header="Handle2" Binding="{Binding Path=Handle}" CellStyle="{StaticResource DirtyStatus}"/>
How can i access this property instead of column Header
-->
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding Path=TVM}"
Grid.Row="0" Grid.Column="0"
AutoGenerateColumns="False"
Name="dataGridCarHistResult" >
<DataGrid.Columns>
<DataGridTextColumn Header="HandleHeader" Binding="{Binding Path=Handle}" CellStyle="{StaticResource DirtyStatus}"/>
<DataGridTextColumn Header="Dirty" Binding="{Binding Path=Dirty.Count}" CellStyle="{StaticResource DirtyStatus}"/>
<DataGridTextColumn Header="PriceHeader" Binding="{Binding Path=Price}" />
<DataGridTextColumn Header="QuantityHeader" Binding="{Binding Path=Quantity}" CellStyle="{StaticResource DirtyStatus}"/>
<DataGridTextColumn Header="TransmissionMethodHeader" Binding="{Binding Path=TransmissionMethod}"/>
</DataGrid.Columns>
</DataGrid>
<Button Content="Change Quantity" Click="Button_Click" Grid.Row="1" Grid.Column="0" />
<Button Content="Change Handle" Click="ButtonHandle_Click" Grid.Row="2" Grid.Column="1" />
<Button Content="Reload" Click="Button_Reload_Click" Grid.Row="3" Grid.Column="1" />
</Grid>
转换器代码
public class isDirtyConverter : IMultiValueConverter
{
public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return false;
if (value.Length < 2)
return false;
if (value[0] == null || value[1] == null)
return false;
string columnName = "";
IList dirtyColumns = value[0] as IList;
columnName = value[1] as string;
if (dirtyColumns == null || columnName == null)
return false;
if (dirtyColumns.Contains(columnName.ToUpper()))
return true;
return false;
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}