如何根据状态更改 WPF TextBox 中文本的颜色?

问题描述

我正在尝试为 TextBox 设计一个自定义模板,它会根据状态更改其颜色。我目前的风格如下:

<Style targettype="{x:Type TextBox}">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="MinWidth" Value="120" />
        <Setter Property="MinHeight" Value="20" />
        <Setter Property="AllowDrop" Value="true" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate targettype="{x:Type TextBoxBase}">
                    <Border Name="Border" Padding="2" BorderThickness="1"
                            Background="{StaticResource TextBoxnormalBackgroundBrush}"
                            BorderBrush="{StaticResource TextBoxnormalBorderBrush}">
                        <ScrollViewer Margin="0" x:Name="PART_ContentHost" Foreground="{StaticResource TextBoxnormalForegroundBrush}" />

                        <visualstatemanager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="normal">
                                    <Storyboard>
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="Background" To="{StaticResource TextBoxnormalBackgroundBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush" To="{StaticResource TextBoxnormalBorderBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="PART_ContentHost" Storyboard.TargetProperty="(TextBlock.Foreground)" To="{StaticResource TextBoxnormalForegroundBrush}" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="disabled">
                                    <Storyboard>
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="Background" To="{StaticResource TextBoxdisabledBackgroundBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush" To="{StaticResource TextBoxdisabledBorderBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="PART_ContentHost" Storyboard.TargetProperty="(TextBlock.Foreground)" To="{StaticResource TextBoxdisabledForegroundBrush}" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="ReadOnly">
                                    <Storyboard>
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="Background" To="{StaticResource TextBoxdisabledBackgroundBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush" To="{StaticResource TextBoxdisabledBorderBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="PART_ContentHost" Storyboard.TargetProperty="(TextBlock.Foreground)" To="{StaticResource TextBoxdisabledForegroundBrush}" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="MouSEOver">
                                    <Storyboard>
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="Background" To="{StaticResource TextBoxHoverBackgroundBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush" To="{StaticResource TextBoxHoverBorderBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="PART_ContentHost" Storyboard.TargetProperty="(TextBlock.Foreground)" To="{StaticResource TextBoxHoverForegroundBrush}" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused">
                                </VisualState>
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="Background" To="{StaticResource TextBoxFocusedBackgroundBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="BorderBrush" To="{StaticResource TextBoxFocusedBorderBrush}" />
                                        <a:BrushSwitchAnimation Storyboard.TargetName="PART_ContentHost" Storyboard.TargetProperty="(TextBlock.Foreground)" To="{StaticResource TextBoxFocusedForegroundBrush}" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </visualstatemanager.VisualStateGroups>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

当应用程序运行时,我可以在 VS 中验证,PART_ContentHostForeground 设置为我想要的值(白色,在这种情况下),但文本保持黑色。

如何使用 TextBox 更改 VisualState 内文本的颜色?

作为参考,BrushSwitchAnimation(虽然它不相关,因为它工作正常):

public class BrushSwitchAnimation : AnimationTimeline
  {
    static BrushSwitchAnimation()
    {
      DurationProperty.OverrideMetadata(typeof(BrushSwitchAnimation),new FrameworkPropertyMetadata(new Duration(TimeSpan.Zero)));
    }

    protected override Freezable CreateInstanceCore()
    {
      return new BrushSwitchAnimation();
    }

    public override object GetCurrentValue(object defaultOriginValue,object defaultDestinationValue,AnimationClock animationClock)
    {
      if (!animationClock.CurrentProgress.HasValue)
        return Brushes.Transparent;

      return animationClock.CurrentProgress.Value < 0.5 ? 
        From ?? (defaultOriginValue as Brush) : 
        To ?? (defaultDestinationValue as Brush);
    }

    public override Type TargetPropertyType => typeof(Brush);

    #region From dependency property

    public Brush From
    {
      get { return (Brush)GetValue(FromProperty); }
      set { SetValue(FromProperty,value); }
    }

    public static readonly DependencyProperty FromProperty =
        DependencyProperty.Register("From",typeof(Brush),typeof(BrushSwitchAnimation));

    #endregion

    #region To dependency property

    public Brush To
    {
      get { return (Brush)GetValue(ToProperty); }
      set { SetValue(ToProperty,value); }
    }

    public static readonly DependencyProperty ToProperty =
        DependencyProperty.Register("To",typeof(BrushSwitchAnimation));

    #endregion
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)