繁体   English   中英

设置 WPF UserControl 的样式

[英]Setting the style of a WPF UserControl

我知道我可以通过添加一个属性在UserControl像这样设置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>

但是有没有一种方法可以直接在ResourceDictionary设置UserControl的样式,例如:

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

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

然而,这只设置了UserControlBackground ,所以我如何将相同的背景应用到它的组件。

我怎样才能用UserControl做到这一点?

您可以像这样直接设置 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>

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

从 MSDN 引用Style.TargetType 属性

将 TargetType 属性设置为 TextBlock 类型而不设置 x:Key 将 x:Key 隐式设置为 {x:Type TextBlock}。 这也意味着,如果您给 [...] 样式一个 x:Key 值而不是 {x:Type TextBlock},样式将不会自动应用于所有 TextBlock 元素。 相反,您需要将样式显式应用于 TextBlock 元素。

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

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

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

<Application ...
MainWindow="MainWindow">

或在启动事件期间...

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

资源在 MainWindow 的每个控件中都可用。

要为特定用户控件设置样式,请查看此处: 为用户控件设置样式

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

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

    //.. my other components
</UserControl>

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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