Search code examples
win-universal-appwindows-10visualstatemanagerimagesource

VisualState Setter with CustomControl not working


I hava a UserControl that Contains a Button and a Image Control and a Property like this:

 public sealed partial class ImageButton : UserControl
        {
            public ImageSource Source { get { return Image.Source; } set { Image.Source = value; } }
        }

Setting the ImageSource in XAML works fine like this:

<views:ImageButton x:Name="MyButton" Source="../Assets/image.jpg"/>

But when I try to set it in VisualStateManager it breaks the complete State:

<Setter Target="MyButton.Source" Value="../Assets/image.jpg"/>

As usual Windows spits out no (helpful) error message, so I have no idea what is wrong here. Can anyone help?


Solution

  • Okay, found it out myself: You need to register the Source Property as DependencyProperty

    public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source",
                typeof (ImageSource), typeof (ImageButton), new PropertyMetadata(string.Empty, OnPropertyChanged));
    
    private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
                {
                    ((ImageButton) d).Source = (ImageSource) e.NewValue;
                }