wpf custom button best approach

后端 未结 1 834
忘掉有多难
忘掉有多难 2020-11-30 09:11

I want to create a custom Button inside WPF. Of course, the button will be an UserControl and it will contain many visual elements (like stroke, highlight, shad

1条回答
  •  抹茶落季
    2020-11-30 09:52

    Like you, when I was getting started and wanted to understand how / what was going on and working with templates, it took a lot of trial and error. Hopefully my research and some step-by-step components can help you customize to your liking and KNOWING where things are coming from.

    First, when trying to understand how a new "template style" will work, I created a simple stand-alone WPF app ("AMS") for my Any Manipulating Styles. This way, I don't have to wait forever to see what something will look like during trial / error with the rest of my primary project and themes.

    From that, I created a new WPF Window called "TestingStyles". Save/Compile, run, no problem.

    Now, in the "VIEW CODE" of the TestingStyles window, I have put whatever I am playing with for a custom class... To help show the step-by-step, I've created the following:

    namespace AMS
    {
        /// 
        /// Interaction logic for TestingStyles.xaml
        /// 
        public partial class TestingStyles : Window
        {
            public TestingStyles()
            {
                InitializeComponent();
            }
        }
    
        // Enumerator for a custom property sample...
        public enum HowToShowStatus
        {
            ShowNothing,
            ShowImage1
        }
    
    
        public class YourCustomButtonClass : Button
        {
            public YourCustomButtonClass()
            {
                // auto-register any "click" will call our own custom "click" handler
                // which will change the status...  This could also be done to simplify
                // by only changing visibility, but shows how you could apply via other
                // custom properties too.
                Click += MyCustomClick;
            }
    
            protected void MyCustomClick(object sender, RoutedEventArgs e)
            {
                if( this.ShowStatus == HowToShowStatus.ShowImage1 )
                    this.ShowStatus = HowToShowStatus.ShowNothing;
                else
                    this.ShowStatus = HowToShowStatus.ShowImage1;
            }
    
    
            public static readonly DependencyProperty ShowStatusProperty =
                  DependencyProperty.Register("ShowStatus", typeof(HowToShowStatus),
                  typeof(YourCustomButtonClass), new UIPropertyMetadata(HowToShowStatus.ShowNothing));
    
            public HowToShowStatus ShowStatus
            {
                get { return (HowToShowStatus)GetValue(ShowStatusProperty); }
                set { SetValue(ShowStatusProperty, value); }
            }
        }
    
    }
    

    As you can see, the custom "Button" class, I have at the bottom outside the default TestingStyles : Window declaration... so its all in the same "Project".

    In this XAML sample, I make reference to a "TaskComplete.png" graphic file (which should just for sample purposes, add directly to the project... Even if a simple smiley face for sample purposes). So, create such a simple .png file... even by using Microsoft Paint and drawing a circle with eyes and smile. Save into the project at the root (get into pathing stuff later, get it working first).

    Save and recompile the project, so the project knows publicly what the new "class" (button) is when you start to define the XAML template.

    Now, back to the TestingStyles designer and get it into split screen so you can see both the designer and the XAML markup... and Just replace with the following...