12

I know I can set the style of a UserControl like so in the control by adding an attribute:

Style="{StaticResource MyStyle}"

And having a style in my ResourceDictionary that looks something like the following:

<Style x:Key="MyStyle" TargetType="{x:Type UserControl}">
    <Style.Resources>
        <Style TargetType="Label">
            <!-- Label Setters -->
        </Style>
        <Style TargetType="TextBox">
            <!-- TextBox Setters -->
        </Style>
    </Style.Resources>
</Style>

But is there a way I can set the style of the UserControl in the ResourceDictionary directly like:

<Style x:Key="MyStyle" TargetType="{x:Type MyControl}">

Essentially my question is, can I apply the style directly to the control instead of to the controls components?

EDIT: What I am trying to accomplish is something like the following:

<Style x:Key="MyStyle" TargetType="{x:Type MyControl}">
    <Setter Property="Background" Value="Black"/>
</Style>
<Style x:Key="{x:Type MyControl}" TargetType="{x:Type MyControl}" BasedOn="{StaticResource MyStyle}"/>

Where the second line applies the style to all controls in the application, if you do something similar with a normal control this approach works.

However this only sets the Background of the UserControl, so how can I apply that same background to its components.

How can I do it with the UserControl?

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69

5 Answers5

10

You can directly set the UserControl's Style like this:

<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
    <UserControl.Style>
        <Style>
            <Setter Property="local:MyControl.MyProperty" Value="..."/>
            ...
        </Style>
    </UserControl.Style>
</UserControl>

or like this:

<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
    <UserControl.Style>
        <Style TargetType="local:MyControl">
            <Setter Property="MyProperty" Value="..."/>
            ...
        </Style>
    </UserControl.Style>
</UserControl>

A default Style in the UserControl's Resources should also work:

<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
    <UserControl.Resources>
        <Style TargetType="local:MyControl">
            <Setter Property="MyProperty" Value="..."/>
            ...
        </Style>
    </UserControl.Resources>
</UserControl>
Clemens
  • 123,504
  • 12
  • 155
  • 268
3

You need to remove the x:Key from your defined style so that it can be applied universally to all controls of the same type as what is defined in the TargetType.

To quote from MSDN for Style.TargetType Property:

Setting the TargetType property to the TextBlock type without setting an x:Key implicitly sets the x:Key to {x:Type TextBlock}. This also means that if you give the [...] Style an x:Key value of anything other than {x:Type TextBlock}, the Style would not be applied to all TextBlock elements automatically. Instead, you need to apply the style to the TextBlock elements explicitly.

slugster
  • 49,403
  • 14
  • 95
  • 145
  • Always the simple things. Is there a way to apply the `UserControl` background to its components i.e. the Label without having to set the `Style.Resources` like I have done in my example? – TheLethalCoder Mar 18 '16 at 11:23
  • Interestingly, the [*Background*](https://msdn.microsoft.com/en-us/library/system.windows.controls.control.background(v=vs.110).aspx) dependency property `Inherits` metadata is not set to `true`, however the default value is `Brushes.Transparent`. IOW: unless the constituent controls have had their background explicitly defined inline or implicitly overridden (like you are doing with your custom control) then you should be fine, no further change should be necessary. – slugster Mar 18 '16 at 12:01
  • So because I have set a `Label` style I will need to set the style in the `Style.Resources` element to override this? – TheLethalCoder Mar 18 '16 at 12:08
  • For example I have style like so ` – TheLethalCoder Mar 18 '16 at 12:18
2

Necro answer for a special case. If the user control is selected via a DataTemplate resource in another WPF control or window, WPF may not automagically apply a default style from an imported resource dictionary. However, you can apply named style resource after importing a resource dictionary.

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../../Resources/ResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
<UserControl.Style>
    <Binding Source="{StaticResource MyUserControlStyle}"></Binding>
</UserControl.Style>
Heather V.
  • 160
  • 1
  • 10
0

To style all controls, add your ResourceDictionary to the resources of your App.xaml.

<Application.Resources>
 <!-- Your Resources for the whole application here -->
</Application.Resources>

If your open your Mainwindow with the App...

<Application ...
MainWindow="MainWindow">

or during the startup event...

<Application ...
MainWindow="MainWindow">
Startup="Application_Startup">

the resources are available in every control of your MainWindow.

To set the style for a specific usercontrol look here: Set Style for user control

Community
  • 1
  • 1
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
  • I know I can do that, My question is how can I create a style to the control not how can I add the resource dictionary to the application – TheLethalCoder Mar 18 '16 at 10:57
0

in your user control xaml place the style inside the resources tag:

<UserControl>
    <UserControl.Resources>
       <Style ...</Style>
    </UserControl.Resources>

    //.. my other components
</UserControl>
JJ_Coder4Hire
  • 4,706
  • 1
  • 37
  • 25