6

我有一个DataGrid包含几个DataGridTemplateColumns. 我的问题是当前选定的行会将某些单元格前景变为白色,即使文本变为白色。 DataGridTemplateColumns包含 TextBlocks 的行为正确并变为白色,而DataGridTemplateColumns包含 TextBoxs 在选择行时不会更改。

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

我已经尝试过这个解决方案:但它只能让 TextBlocks 受到影响,有人知道可能出了什么问题吗?

4

1 回答 1

5

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

<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>
于 2010-12-06T15:40:26.293 回答