WPF:如何将属性值继承到所有子控件?

发布于 2024-08-04 11:48:18 字数 130 浏览 5 评论 0原文

我有包含其他控件的用户控件。我希望如果我为 UserControl 设置前景色,所有子控件都会自动继承它。我对字体样式/大小也有同样的问题。

我可以以某种方式将这些属性设置为自动/继承吗?是否可以在没有循环的情况下设置所有子控件?

I have UserControls containing other controls. I want that if I set a Foreground color for the UserControl, all child controls automatically inherit it. I have the same problem with font style/size.

Can I set these properties somehow to auto/inherit? Is this possible to set all subcontrols without a loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-08-11 11:48:18

您可以创建资源字典来全局定义默认样式。

您还可以引用资源字典或在任何对象中定义样式。

在任何一种情况下,这些样式都将应用于没有显式定义样式的所有子对象...

示例:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <!--Default styles that will apply to any object of the specified type (if it doesn't have style set locally)-->
    <Style TargetType="Label" >
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Padding" Value="0"/>
    </Style>
</ResourceDictionary>

You can you create resource dictionaries to define default styles globally.

You can also reference a resource dictionary or define a style in any object.

In either case those styles will apply to all child objects that don't have a style explicitly defined...

Example:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <!--Default styles that will apply to any object of the specified type (if it doesn't have style set locally)-->
    <Style TargetType="Label" >
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Padding" Value="0"/>
    </Style>
</ResourceDictionary>
童话里做英雄 2024-08-11 11:48:18

研究使用样式和 BasedOn 设置。

我最近写了一个类似的示例,位于 here 。不幸的是,这个问题与 Silver Lite 有关,所以没有回答这个问题,但我认为它可能会给您一些关于去哪里寻找的想法。

Look into using Styles and BasedOn settings.

I recently wrote an example of something similar located here. Unfortunally the question was related to Silver Lite so didn't answer the question, but I think it may give you some ideas on where to look.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文