我有一个DataGridTemplateColumn定义如下。如果选择了行,我需要将两个文本块的Foreground属性更改为White
<DataGrid.Columns>
<DataGridTemplateColumn Header="User" Width="240" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Margin="10,3,0,0" Foreground="#1c72c7" >
<Run Text="{Binding FullName, Mode=OneWay}" />
</TextBlock>
<Label Padding="0,0,0,0" Margin="0,0,0,3">
<TextBlock Foreground="#1c72c7" Margin="10,0,0,0" TextDecorations="Underline" Text="{Binding DisplayName}" />
</Label>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我已经定义了RowStyle以更改行的背景颜色,如下所示
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}" />
</Style.Resources>
</Style>
答案 0 :(得分:6)
您应该为每个人Style
修改TextBlock
,并按照您的喜好添加行为:
<Style x:Key="FirstTextBlockStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected,
RelativeSource= {RelativeSource
AncestorType={x:Type DataGridRow}}}"
Value="True">
<Setter Property="Foreground" Value="White" />
</DataTrigger>
</Style.Triggers>
</Style>