0

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

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

4

2 回答 2

3

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

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

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

例子:

<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>
于 2009-09-09T21:14:35.830 回答
1

研究使用 Styles 和 BasedOn 设置。

我最近写了一个类似的例子,位于此处。不幸的是,这个问题与 Silver Lite 有关,所以没有回答这个问题,但我认为它可能会给你一些关于在哪里看的想法。

于 2009-09-09T21:06:46.420 回答