2

我正在使用 WPF。请查看代码中与前景相关的注释。当文本框获得焦点时,字体应更改为白色,但它不起作用。为什么?

 <ControlTemplate TargetType="{x:Type TextBox}">
                <Border x:Name="bg" BorderBrush="#FFDCDCC9" Background="#FFDCDCC9" BorderThickness="1">
                    <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>

                    <Trigger  Property="IsMouseOver" Value="True">
                        <Setter  Property="BorderBrush" TargetName="bg" Value="#FFEC94C0"/>
                        <Setter  Property="Background" TargetName="bg" Value="#FFEC94C0"/>
                        <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                        <Setter Property="Cursor" Value="Hand"/>
                    </Trigger>

                    <Trigger Property="IsFocused" Value="True" >
                        <Setter Property="BorderBrush" TargetName="bg" Value="#FFB5266E"/>
                        <Setter  Property="Background" TargetName="bg" Value="#FFEC94C0"/>
                        <!-- foreground is invalid --> 
                        <Setter  Property="Foreground" TargetName="bg" Value="white"/>
                        <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                        <Setter Property="Cursor" Value="Hand"/>
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
4

1 回答 1

2

边框没有前景。从等式中删除 TargetName ......我相信它会起作用。它应该如下所示(如果这是一个需要密钥的资源)。这对我来说很好。

<ControlTemplate x:Key="a" TargetType="{x:Type TextBox}">
        <Border x:Name="bg" BorderBrush="#FFDCDCC9" Background="#FFDCDCC9" BorderThickness="1">
            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
        </Border>
        <ControlTemplate.Triggers>

            <Trigger  Property="IsMouseOver" Value="True">
                <Setter  Property="BorderBrush" TargetName="bg" Value="#FFEC94C0"/>
                <Setter  Property="Background" TargetName="bg" Value="#FFEC94C0"/>
                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                <Setter Property="Cursor" Value="Hand"/>
            </Trigger>

            <Trigger Property="IsFocused" Value="True" >
                <Setter Property="BorderBrush" TargetName="bg" Value="#FFB5266E"/>
                <Setter  Property="Background" TargetName="bg" Value="#FFEC94C0"/>
                <!-- foreground is invalid -->
                <Setter  Property="Foreground"  Value="white"/>
                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                <Setter Property="Cursor" Value="Hand"/>
            </Trigger>

        </ControlTemplate.Triggers>
    </ControlTemplate>
于 2013-07-08T21:19:46.737 回答