Frage

Ich habe folgende XAML zum Ändern des Bildes für WPF-Taste, wenn die Maus auf der Schaltfläche befindet. Es gibt unten Fehler. Jede Hilfe ist willkommen ...

'System.Windows.Media.Animation.DoubleAnimation' Animation Objekt nicht zu animieren Eigenschaft 'Source' verwendet werden, da es von inkompatiblen Typ 'System.Windows.Media.ImageSource'.

<Style TargetType="{x:Type local:ButtonControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ButtonControl}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation 
                                                Storyboard.TargetName="img"
                                                Storyboard.TargetProperty="Source"
                                                To="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MouseOverImage}"
                                                />
                                    </Storyboard>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                            <Border>
                                <Image x:Name="img"
                                         Source="pack://application:,,,/Recipe_06_13;component/Resources/normal.bmp"
                                />
                            </Border>
                    </Grid>
                </ControlTemplate>
        </Setter.Value>
    </Setter>
    </Style>
War es hilfreich?

Lösung

Die Double wird verwendet, Doppel eine Abhängigkeitseigenschaft vom Typ zu animieren. Daher kann es nicht zu ‚animieren‘ eine Bildquelle verwendet werden. Um das zu erreichen, was Sie tun, würde ich es ein wenig anders gehen. Ich würde die Bildquelle auf eine Eigenschaft des Codes hinter binden, und erfassen die MouseEnter- und Leave Ereignisse diese Eigenschaft zu ändern.

XAML

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="336" Width="354">
    <Grid x:Name="myGrid">
        <Image 
            x:Name="myImage" 
            Stretch="UniformToFill"
            Source="{Binding MyImageSource}" 
            VerticalAlignment="Top" />
    </Grid>
</Window>

Code-Behind:

namespace WpfApplication1
{
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Input;

    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        public Window1()
        {
            this.MyImageSource = "/image_normal.png";
            InitializeComponent();

            this.DataContext = this;

            myImage.MouseEnter += new MouseEventHandler(myGrid_MouseEnter);
            myImage.MouseLeave += new MouseEventHandler(myGrid_MouseLeave);
        }

        private string _myImageSource;
        public string MyImageSource 
        {
            get
            {
                return _myImageSource;
            }
            set
            {
                if (_myImageSource != value)
                {
                    _myImageSource = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MyImageSource"));
                    }
                }
            }
        }

        void myGrid_MouseLeave(object sender, MouseEventArgs e)
        {
            this.MyImageSource = "/image_normal.png";
        }

        void myGrid_MouseEnter(object sender, MouseEventArgs e)
        {
            this.MyImageSource = "/image_hover.png";
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top