DataGridTemplateColumn 行悬停文本框前景

DataGridTemplateColumn row hover textbox foreground

我有一个 DataGridTemplateColumn,里面有一个文本框。当我将鼠标悬停在行上时,除文本框外,所有其他前景都变为白色。当鼠标悬停在行上时,我可以将哪种样式应用于文本框以使其前景更改以匹配其他 textblocks/fonts?

示例XAML

<DataGridTemplateColumn>
  <DataGridTemplateColumn.CellTemplate>
    <TextBox Text="Test 123" />
    <TextBlock Text="Test 123" />
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

造型

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="BorderBrush"
            Value="{x:Null}" />
    <Setter Property="Foreground"
            Value="{StaticResource BlackBrush}" />
    <Style.Triggers>
        <Trigger Property="IsSelected"
                 Value="True">
            <Setter Property="Background"
                    Value="Transparent" />
            <Setter Property="BorderBrush"
                    Value="{x:Null}" />
            <Setter Property="Foreground"
                    Value="{StaticResource BlackBrush}" />
        </Trigger>
        <Trigger Property="IsMouseOver"
                 Value="True">
            <Setter Property="Background"
                    Value="{StaticResource RedBrush}" />
            <Setter Property="Foreground"
                    Value="{StaticResource WhiteBrush}" />
        </Trigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Padding"
            Value="5, 10" />
    <Setter Property="IsTabStop"
            Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border Padding="{TemplateBinding Padding}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="True">
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsSelected"
                 Value="True">
            <Setter Property="Background"
                    Value="{Binding Path=Background, RelativeSource={RelativeSource AncestorType=DataGridRow}}" />
            <Setter Property="Foreground"
                    Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=DataGridRow}}" />
            <Setter Property="BorderBrush"
                    Value="{Binding Path=BorderBrush, RelativeSource={RelativeSource AncestorType=DataGridRow}}" />
        </Trigger>
    </Style.Triggers>
</Style>

您需要为 TextBox 定义额外的 Style:

<Style TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="True">
            <Setter Property="Foreground" Value="White" />
        </DataTrigger>
    </Style.Triggers>
</Style>