WPF DataGridTemplateColumn IsSelected ForgroundColor无法按预期工作

时间:2010-12-06 10:04:10

标签: wpf datagrid datagridtemplatecolumn

我有DataGrid,其中包含多个DataGridTemplateColumns。 我的问题是当前选择的行将一些单元格前景变为白色,即使文本变为白色。 包含TextBlocks的DataGridTemplateColumns行为正常并变为白色,而包含TextBox的DataGridTemplateColumns在选择行时不会更改。

有人知道为什么或如何解决这个问题吗?

我试过这个solution:,但它只会让TextBlocks受到影响,有人知道可能出错吗?

1 个答案:

答案 0 :(得分:5)

我不确定为什么Trigger也不会影响TextBox ForeGround,但通常当单元格处于编辑模式时,选择颜色不应该处于活动状态,这可能是TextBox拒绝该值的原因。我不确定。如果使用DataGridTextColumn并进入编辑模式,您将看到相同的效果,TextBox将不具有触发器的前景但TextBlock将具有前景。要将White ForeGround应用于DataGrid中的所有选定TextBox,您可以执行此操作(请注意,这也将影响处于编辑模式的TextBox)

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