【问题标题】:change label foreground color in visual state在视觉状态下更改标签前景色
【发布时间】:2018-07-05 01:05:38
【问题描述】:

我尝试创建具有标签和文本框的用户控件。 我希望我的控件有自定义的视觉状态集。 所以我在 xaml 中编写了这段代码:

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

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup Name="CommonStates">
        <VisualStateGroup.Transitions>
            <VisualTransition GeneratedDuration="0:0:3" />
        </VisualStateGroup.Transitions>

        <VisualState Name="Normal" />
        <VisualState Name="Focus">
            <Storyboard>
                <ColorAnimation To="Blue" Storyboard.TargetName="LabelControl" Storyboard.TargetProperty="Foreground" />
            </Storyboard>
        </VisualState>
        <VisualState Name="Success" />
        <VisualState Name="Error" />
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>


<StackPanel Name="WrapperControl">
    <Label Name="LabelControl" Content="Binding Label" />
    <TextBox Name="TextBoxControl" Text="{Binding Value}" Validation.Error="TextBox_Error" GotFocus="TextBox_GotFocus"/>
</StackPanel>

在我的 UserControl 类中:

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);
    }
}

所以我很尊重,当我专注于控件的文本框元素时,标签的视觉状态会发生变化,前景色会发生变化。但它不会发生。

【问题讨论】:

    标签: wpf colors user-controls foreground visualstates


    【解决方案1】:

    您需要为此添加一个触发器。

    尝试为鼠标事件定义一个触发器,它会解决你的问题。

    <Style.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsSelected" Value="True" />
            <Condition Property="IsMouseOver" Value="True" />
        </MultiTrigger.Conditions>
        <Setter ... />
    </MultiTrigger>
    

    【讨论】:

    • 感谢您的回答。但我不确定我需要什么。加载控制后,它进入正常状态。当 TextBox 获得焦点时,控件具有焦点状态。而当 TextBox 失去焦点时,Control 可能是 Success 或 Error 两种状态之一。
    【解决方案2】:

    所以几个小时后我找到了解决方案。

    首先,我尝试在 StackPanel 中移动 VisualStateManager.VisualStateGroups 块。然后我得到运行时错误,它解释了 ColorAnimation 无法将颜色设置为 Foreground 属性。所以我把它改成 Foreground.Color。

    这是我的工作 xaml:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 2014-03-14
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多