Xaml继承的样式

时间:2009-04-22 18:01:57

标签: wpf xaml styles

XAML中是否有办法设置适用于所有控件的样式?例如下面我想在我的所有控件上留一个余量。我可以通过将TargetType更改为Button,CheckBox等来为每种类型添加样式。相反,我想设置它,就像我在下面设置从Control继承的所有类型的样式:

<UserControl x:Class="MyPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Height="300" Width="300">
    <UserControl.Resources>
        <Style TargetType="Control">
            <Setter Property="Margin" Value="3"/>
        </Style>
</UserControl.Resources>
    <StackPanel>
        <Button>My Button</Button>
        <CheckBox>My Checkbox</CheckBox>
    </StackPanel>
</UserControl>

1 个答案:

答案 0 :(得分:15)

没有办法做到这一点。但是,您可以定义基本样式并从中继承:

<Style x:Key="BaseStyle">
    <Setter Property="FrameworkElement.Margin" Value="3"/>
</Style>

<Style TargetType="Button" BasedOn="{StaticResource BaseStyle}">
</Style>

<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">
</Style>