Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 when 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?

like image 501
bplus Avatar asked Dec 06 '10 10:12

bplus


1 Answers

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>
like image 153
Fredrik Hedblad Avatar answered Nov 01 '22 04:11

Fredrik Hedblad