WPF DataGridTemplateColumn IsSelected ForgroundColor not working as expected WPF DataGridTemplateColumn IsSelected ForgroundColor not working as expected wpf wpf

WPF DataGridTemplateColumn IsSelected ForgroundColor not working as expected


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>