设置WPF UserControl的样式

时间:2016-03-18 10:44:38

标签: c# wpf user-controls

我知道我可以通过添加属性在控件中设置UserControl的样式:

Style="{StaticResource MyStyle}"

我的ResourceDictionary中的样式看起来如下所示:

<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>

但有没有办法可以直接在UserControl中设置ResourceDictionary的样式:

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

基本上我的问题是,我可以直接将样式应用于控件而不是控件组件吗?

修改 我想要完成的是以下内容:

<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}"/>

如果第二行将样式应用于应用程序中的所有控件,如果您使用普通控件执行类似操作,则此方法有效。

但是,这仅设置Background的{​​{1}},因此如何将相同的背景应用于其组件。

如何使用UserControl

进行操作

5 个答案:

答案 0 :(得分:2)

您可以直接设置UserControl的样式:

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

或者像这样:

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

UserControl资源中的默认样式也应该起作用:

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

答案 1 :(得分:1)

您需要从定义的样式中删除x:Key,以便它可以普遍应用于与TargetType中定义的相同类型的所有控件。

从MSDN引用Style.TargetType Property

  

将TargetType属性设置为TextBlock类型而不设置x:Key隐式将x:Key设置为{x:Type TextBlock}。 这也意味着如果您为[...]样式赋予除{x:Type TextBlock}以外的任何内容的x:Key值,则Style将不会自动应用于所有TextBlock元素。相反,您需要明确地将样式应用于TextBlock元素。

答案 2 :(得分:1)

在您的用户控件xaml中将样式放在资源标记内:

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

    //.. my other components
</UserControl>

答案 3 :(得分:0)

要设置所有控件的样式,请将ResourceDictionary添加到App.xaml的资源中。

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

如果您使用应用程序打开主窗口...

<Application ...
MainWindow="MainWindow">

或在启动活动期间......

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

您的MainWindow的每个控件都可以使用这些资源。

要在此处设置特定用户控件的样式,请执行以下操作: Set Style for user control

答案 4 :(得分:0)

特殊情况下的Necro答案。如果通过另一个WPF控件或窗口中的DataTemplate资源选择了用户控件,则WPF可能不会自动从导入的资源字典中应用默认样式。但是,您可以在导入资源字典后应用命名样式资源。

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../../Resources/ResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
<UserControl.Style>
    <Binding Source="{StaticResource MyUserControlStyle}"></Binding>
</UserControl.Style>