WPF custom derived control style

前端 未结 5 1241
难免孤独
难免孤独 2021-01-04 22:04

I have a custom control derived from Button:

    class MyControl : Button{}

And suppose, this class is empty (has no members).

In t

5条回答
  •  旧巷少年郎
    2021-01-04 22:57

    You override the DefaultStyleKey's metadata in your static constructor:

    static MyControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(MyControl),
            new FrameworkPropertyMetadata(typeof(MyControl)));
    }
    

    Then, in your resources, you can base its style on the button: