简体   繁体   中英

Setting the style of a WPF UserControl

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 ?

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>

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.

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

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

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

    //.. my other components
</UserControl>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM