WPF DataGridTemplateColumn IsSelected ForgroundColor not working as expected
I have a DataGrid
that contains several DataGridTemplateColumns
.
My problem is that the currently selected row will turn some of the cells foregrounds to white, i.e. make the text white.
DataGridTemplateColumns
that contain TextBlocks behave correctly and turn white while DataGridTemplateColumns
that contain TextBoxs do not change whe开发者_如何学Gon the row is selected.
Does anybody know why or how to fix this problem?
I have tried this solution: but it can only get TextBlocks to be affected, does anybody know what might be wrong?
I'm not really sure why the Trigger won't effect a TextBox ForeGround as well but normally the selection color shouldn't be active when the cell is in edit mode so that might be the reason the TextBox rejects the value but I'm not sure. You'll see the same effect if you use a DataGridTextColumn and enter edit mode, the TextBox won't have the foreground from the Trigger but the TextBlock will. To apply a White ForeGround to all selected TextBoxes in the DataGrid you can do this (note that this will also effect a TextBox that's in edit mode)
<DataGrid ...>
<DataGrid.Resources>
<Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}" >
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
<!-- Workaround for the TextBox -->
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}, Path=IsSelected}" Value="True">
<Setter Property="Foreground" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<!-- ... -->
</DataGrid>
精彩评论