[英]DataGridCell style based on DataGrid property
I want to add a style to the DataGridCell
of a custom DataGrid
which is based on property of that DataGrid
. 我想向基于该
DataGrid
属性的自定义DataGrid
的DataGridCell
添加样式。 For example I want to have a DependencyProperty named VisualizeReadOnlyCells and if that property is set to True, I want the DataGridCell
that is ReadOnly is drawn with gray background. 例如,我想要一个名为VisualizeReadOnlyCells的DependencyProperty,并且如果该属性设置为True,则我希望使用灰色背景绘制ReadOnly的
DataGridCell
。 And other similar things. 和其他类似的东西。 As an workaround I have created two different styles for the
DataGridCell
and I assign those styles to the CellStyle
property of the DataGrid
in the DataGrid
style definition. 作为一种解决方法,我为
DataGridCell
创建了两种不同的样式,并将这些样式分配给DataGrid
样式定义中DataGrid
的CellStyle
属性。 But with more properties the number of such styles will grow and the code would be very unmanageable. 但是随着更多的属性,此类样式的数量将会增加,并且代码将非常难以管理。
My current solution is: 我当前的解决方案是:
<Style x:Key="DataGridCellNoVisualize" TargetType="DataGridCell">
- style definition
</Style>
<Style x:Key="DataGridCellVisualize" TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Background" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="DataGridStyle" TargetType="MyCustomDataGrid">
<Setter Property="CellStyle" Value="{StaticResource DataGridCellNoVisualize"/>
<Style.Triggers>
<Trigger Property="VisualizeReadOnlyCells" Value="True">
<Setter Property="CellStyle" Value="{Static Resource DataGridCellVisualize"/>
</Trigger>
</Style.Triggers>
</Style>
What I would like to get is something like this: 我想得到的是这样的:
<Style x:Key="DataGridCellUniversalStyle" TargetType="DataGridCell">
- style definition
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsReadOnly" Value="True"/>
<Condition Property="MyParentDataGrid.VisualizeReadOnlyCells" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Background" Value="Gray"/>
</MultiTrigger.Setters>
</MultiTrigger>
</Style.Triggers>
</Style>
If I understand your issue correctly, you could use a MultiDataTrigger
and bind to the DataGridCell
and the parent DataGrid
: 如果我正确理解了您的问题,则可以使用
MultiDataTrigger
并绑定到DataGridCell
和父DataGrid
:
<Style x:Key="DataGridCellUniversalStyle" TargetType="DataGridCell">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True"/>
<Condition Binding="{Binding VisualizeReadOnlyCells, RelativeSource={RelativeSource AncestorType=local:MyCustomDataGrid}}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="Gray"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.