![](/img/trans.png)
[英]WPF DataGrid with cell style — different cell style in same column
[英]Datagrid Template Column Cell Style Trigger not working
我有一个数据网格模板列,我在其中更改文本块“MyTextBlockRotationVersion”后面代码的文本颜色。 我正在尝试使用文本颜色更改来更改单元格的背景颜色,以便当它从单元格模板变为单元格编辑模板时,反之亦然,红色背景仍然存在。 不知道为什么触发器没有将背景变为红色。 现在文本颜色会改变,但背景不会。 在进入单元编辑模板并返回单元模板后,我失去了颜色变化。
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="MyTextBlockRotationVersion" Text="{Binding RotationVersion, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Center" MaxWidth="70"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsEditing" Value="True" />
</Trigger>
<Trigger x:Name="MyTextBlockRotationVersion" Property="Foreground" Value="Red">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridTemplateColumn.CellStyle>
我认为您不能从另一个DataTemplate
(或ControlTemplate
)中访问元素
还要按名称访问特定元素,您要使用SourceName
而不是x:Name
( x:Name
命名触发器)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.