使用converterParameter的数据网格单元格样式
本文关键字:网格 单元格 样式 数据网 数据 converterParameter 使用 | 更新日期: 2023-09-27 18:07:50
如何在数据网格单元格的样式设置器中使用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道具都绑定了这个全局转换器有两个原因:
- 更容易维护
- 某些列需要根据其他项值的值来启用
您应该使用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>