0

I've got a custom control in WPF, which has a variety of dependency properties that allow visual customization. For the sake of brevity I won't post the entire control, but it basically is setup like this:

<UserControl.Resources>
    <Style TargetType="{x:Type MyControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MyControl}">
                    <Border BorderBrush="{TemplateBinding BorderColor}">
                        // more stuff here
                        <ContentPresenter/>
                    </Border>
                </ControlTemplate>
            </Setter>
        </Setter>
    </Style>
</UserControl.Resources>

The BorderColor property works fine if I set it directly, like this:

<ctl:MyControl BorderColor="Brushes.Red">....</ctl:MyControl>

But I want to set it application-wide. The problem I have is if I simply set the style with no key, it does not apply. Like this:

<Window.Resources>
    <Style TargetType="{x:Type ctl:MyControl}">
        <Setter Property="BorderColor" Value="Brushes.Red"/>
    </Style>
</Window.Resources>

This does not do anything to the control. So I thought I'd just set a key and apply that style, like this:

<Style TargetType="{x:type ctl:MyControl}" x:Key="myStyle">....</Style>

<ctl:MyControl Style="{StaticResource myStyle}">.....</ctl:MyControl>

But this causes the control to vanish, I'm assuming because it's removing the Template. What am I doing wrong? With other framework controls you can just set the properties you want without losing the control template.

amnesia
  • 1,956
  • 2
  • 18
  • 36

1 Answers1

1

You need to inherit from the default style you have created.

inherit style from default style

Community
  • 1
  • 1
James Willock
  • 1,999
  • 14
  • 17
  • With no key, I still had no change. If I set a key and set the style then I get an static resource exception saying my control type doesn't exist. I'm wondering, could this be because I defined my control template within the UserControl.Resources of the control I made, rather than a resource dictionary? – amnesia Aug 25 '15 at 17:03
  • Yes, it's private. You need to move it out, you can put it in your App.xaml, or typically, if you use the Visual Studio menu item to create your control it will create a Themes/Generic.xaml for you, with an empty template. – James Willock Aug 26 '15 at 07:59
  • Accepted answer, though if anyone else stumbles upon this please note that this won't work if your control template is defined in the private resources of your control itself. – amnesia Aug 26 '15 at 11:14