如何解决如何根据状态更改 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_ContentHost
的 Foreground
设置为我想要的值(白色,在这种情况下),但文本保持黑色。
如何使用 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 举报,一经查实,本站将立刻删除。