DataGridTemplateColumn行悬停文本框前景

时间:2017-11-07 13:42:46

标签: wpf xaml styling datagridtemplatecolumn

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

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

Screenshot

1 个答案:

答案 0 :(得分:2)

您需要为Style定义一个额外的TextBox

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