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

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

有帮助吗?

解决方案

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

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

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

示例:

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

其他提示

使用Styles和BasedOn设置进行研究。

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top