views:

658

answers:

3

I have a custom control derived from Button:

    class MyControl : Button{}

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

In the application's main window resources I use ResourceDictionary that contains styles for most WPF controls (so called theme):

    <ResourceDictionary Source="BureauBlue.xaml" />

So, all controls on the window look like it is defined in that theme file. But the styles are not affected on MyControl controls. How can I do MyControl to look same as a Button controls?

Update: The style for Button in BureauBlue.xaml has no key and is defined in the following way:

    <Style TargetType="{x:Type Button}" BasedOn="{x:Null}"> ...</Style>
A: 

Check if style for Button is there in your BureauBlue.xaml and remove x:key attribute from the style if it is there

viky
The style for Button in BureauBlue.xaml has no key and is defined following wise:<Style TargetType="{x:Type Button}" BasedOn="{x:Null}">...</Style>
Zim
+1  A: 

The TargetType property doesn't work for classes that derive from the specified type. See this question

Bryce Kahle
A: 

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:

<Style TargetType="{x:Type lcl:MyControl}" BasedOn="{StaticResource {x:Type Button}}" />

I've tried in the past to override the DefaultStyleKey's metadata to point to the base class (Button in your case), but it doesn't seem to work.

Abe Heidebrecht
Thanks, that works, but strange: if I define such style in Themes/Generic.xaml this gives no effect. And if I define this style in my Window or App resources, then everything is ok.So, it is unclear to me the situation when I create my ButtonEx control in one assembly and will use it from another one, so how could I then define in my control library that my ButtonEx control should look as Button by default?
Zim