Wpf 在视觉状态下更改标签前景色
我尝试创建带有标签和文本框的UserControl。 我希望我的控件具有自定义的可视状态集。 所以我用xaml编写了这段代码:Wpf 在视觉状态下更改标签前景色,wpf,colors,user-controls,foreground,visualstates,Wpf,Colors,User Controls,Foreground,Visualstates,我尝试创建带有标签和文本框的UserControl。 我希望我的控件具有自定义的可视状态集。 所以我用xaml编写了这段代码: 所以,当我在控件的文本框元素中聚焦时,标签的视觉状态将改变,前景颜色将改变。但它不会发生。您需要为此添加一个触发器 尝试为鼠标事件定义触发器,它将解决您的问题 <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Pro
所以,当我在控件的文本框元素中聚焦时,标签的视觉状态将改变,前景颜色将改变。但它不会发生。您需要为此添加一个触发器 尝试为鼠标事件定义触发器,它将解决您的问题
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<Setter ... />
</MultiTrigger>
所以几个小时后我找到了解决办法 首先,我尝试将VisualStateManager.VisualStateGroups块移动到StackPanel中。然后我得到了运行时错误,它解释了ColorAnimation不能将颜色设置为前景属性。所以我把它改为前台颜色 这是我的工作xaml:
谢谢您的回答。但我不确定我需要什么。加载控件后,它将进入正常状态。当TextBox获得焦点时,控件具有焦点状态。当文本框失去焦点时,控制可能是两种状态中的一种成功或错误。
public partial class TextField : UserControl
{
public static readonly DependencyProperty LabelProperty;
public static readonly DependencyProperty ValueProperty;
static TextField() {
LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(TextField));
ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(TextField));
}
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
public string Value
{
get
{
return (string)GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
}
}
public TextField()
{
InitializeComponent();
WrapperControl.DataContext = this;
}
private void TextBox_Error(object sender, ValidationErrorEventArgs e)
{
VisualStateManager.GoToState(this, "Error", true);
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
VisualStateManager.GoToState(this, "Focus", true);
VisualStateManager.GoToState(LabelControl, "Focus", true);
}
}
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<Setter ... />
</MultiTrigger>
<UserControl x:Class="OgameApp.Components.TextField"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OgameApp.Components"
mc:Ignorable="d"
d:DesignWidth="500"
Validation.ErrorTemplate="{x:Null}">
<UserControl.Resources>
<Style TargetType="Label">
<Setter Property="FontSize" Value="16" />
<Setter Property="Padding" Value="0 0 0 5" />
<Setter Property="Foreground" Value="#FF6D6D6D" />
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="16" />
</Style>
</UserControl.Resources>
<StackPanel Name="WrapperControl" Loaded="WrapperControl_Loaded">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualState Name="Normal" />
<VisualState Name="Focus">
<Storyboard>
<ColorAnimation Storyboard.TargetName="LabelControl" Storyboard.TargetProperty="Foreground.Color" To="Blue" Duration="0:0:3" />
</Storyboard>
</VisualState>
<VisualState Name="Success" />
<VisualState Name="Error" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Label Name="LabelControl" Content="Binding Label" />
<TextBox Name="TextBoxControl" Text="{Binding Value}" Validation.Error="TextBox_Error" GotFocus="TextBox_GotFocus"/>
</StackPanel>