How to animate an Image in a button to shake every 30 seconds in WPF?

后端 未结 2 745
清歌不尽
清歌不尽 2021-01-17 02:37

I not good when it comes to dealing with anything with styles and animations.

I was hoping to be able to get some help on making an Image that is the only content o

2条回答
  •  抹茶落季
    2021-01-17 03:18

    Create a WPF custom control by adding a new item in VS and then navigating to the WPF templates. This will allow you to select "Custom Control (WPF)". Name it "ShakyImageControl". This will create a Themes folder with a generic.xaml in it and a "ShakyImageControl.cs" class file. In the generic.xaml replace the existing style with the following:

    
    

    In the ShakyImageControl class add a dependency property as follows:

    static ShakyImageControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ShakyImageControl), new FrameworkPropertyMetadata(typeof(ShakyImageControl)));
        }
    
        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ShakyImageControl), new UIPropertyMetadata(null));
    

    To use the shakyImage in a button just do:

    
    

    local is an xml namespace like "xmlns:local="clr-namespace:WpfApplication6"

    NB: your custom control can be in a seperate assembly if you want

提交回复
热议问题