如何在datagrid单元格的样式设置器中使用converterParameter? 我需要知道转换器中的元素名称或属性名称。
XAML:
<Window.Resources>
<view:IsEnabledCellConverter x:Key="IsEnabledCellConverter"/>
<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="IsEnabled"
Value="{Binding
Path=Item,
Converter={StaticResource ResourceKey=IsEnabledCellConverter},
ConverterParameter={?????}}"
RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}} />
</Style>
.../...
<Datagrid>
<DataGrid.Columns>
<DataGridTextColumn x:Name="Design"
CellStyle="{StaticResource CellStyle}"
Value="{Binding Path=Design}"/>
<DataGridTextColumn x:Name="FooBar"
CellStyle="{StaticResource CellStyle}"
Value="{Binding Path=Foobar}"/>
</DataGrid.Columns>
</Datagrid>
转换器类:
class IsEnabledCellConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string p = (string)parameter;
MyItem item = (MyItem)value;
switch (p)
{
case "Design":
return string.IsNullOrEmpty(item.Reference);
break;
case "FooBar":
return item.Something != true;
break;
default:
return true;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
编辑:我添加了一些细节来了解需求。 所以我的所有列IsEnabled道具都绑定了这个全局转换器有两个原因:
答案 0 :(得分:2)
您应该使用DisplayIndex
或Header
,并相应地设置ConverterParameter
。
<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding Column.DisplayIndex, RelativeSource={RelativeSource Self}}" Value="0">
<Setter Property="IsEnabled"
Value="{Binding
Path=Item,
Converter={StaticResource ResourceKey=IsEnabledCellConverter},
ConverterParameter=0,
RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" />
</DataTrigger>
<DataTrigger Binding="{Binding Column.DisplayIndex, RelativeSource={RelativeSource Self}}" Value="1">
<Setter Property="IsEnabled"
Value="{Binding
Path=Item,
Converter={StaticResource ResourceKey=IsEnabledCellConverter},
ConverterParameter=1,
RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" />
</DataTrigger>
</Style.Triggers>
</Style>
请告诉我这是否解决了您的问题。