图像不会通过使用视觉状态而改变

Image not change by using visual states

当依赖项 属性 Message 为 NULL 或已填充时,为什么我的视觉状态不起作用?

WPF代码:

<Window x:Class="VisualStateTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:VisualStateTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Border x:Name="border" Background="Black">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="LocationErrorStatusVisualStateGroup">
                <VisualState x:Name="DefaultVisualState">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Image.Source)" Storyboard.TargetName="myImage">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <BitmapImage UriSource="/VisualStateTest;component/Resources/Empty.png"/>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="MessageVisualState">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Image.Source)" Storyboard.TargetName="myImage">
                            <DiscreteObjectKeyFrame KeyTime="0">
                                <DiscreteObjectKeyFrame.Value>
                                    <BitmapImage UriSource="/VisualStateTest;component/Resources/Message.png"/>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Image x:Name="myImage"/>
    </Border>
</Window>

后面的 C# 代码:

using System.Windows;

namespace VisualStateTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Message

        /// <summary>
        /// Message Dependency Property
        /// </summary>
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(MainWindow),
                new FrameworkPropertyMetadata(null,
                    new PropertyChangedCallback(OnMessageChanged)));

        /// <summary>
        /// Gets or sets the Message property. This dependency property 
        /// indicates the message.
        /// </summary>
        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        /// <summary>
        /// Handles changes to the Message property.
        /// </summary>
        private static void OnMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MainWindow target = (MainWindow)d;
            string oldMessage = (string)e.OldValue;
            string newMessage = target.Message;
            target.OnMessageChanged(oldMessage, newMessage);
        }

        /// <summary>
        /// Provides derived classes an opportunity to handle changes to the Message property.
        /// </summary>
        protected virtual void OnMessageChanged(string oldMessage, string newMessage)
        {
            if (newMessage == oldMessage)
                return;

            if (string.IsNullOrEmpty(newMessage))
            {
                VisualStateManager.GoToState(this, "DefaultVisualState", true);
            }
            else
            {
                VisualStateManager.GoToState(this, "MessageVisualState", true);
            }
        }

        #endregion

        public MainWindow()
        {
            Message = "test";
            InitializeComponent();
        }
    }
}

图像的构建操作应设置为 Resource。然后应使用 Pack URIs 引用图像,即

pack://application:,,,/Resources/Empty.png
pack://application:,,,/Resources/Message.png

VisualStateManager有两种使用方法。可以定义视觉状态:

  • 在控件模板中,或者
  • 直接在控件中(就像你在这里所做的那样)

当视觉状态未在模板中定义时,您必须调用 GoToElementState 方法而不是 GoToState 方法。

此外,您必须在实际定义相应视觉状态的控件上调用 GoToElementState 方法,因此您需要在 Border 而不是 [=18= 上调用它]

VisualStateManager.GoToElementState(border, "DefaultVisualState", true);

See here 有关 VisualStateManager

的更多详细信息