4

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

1 回答 1

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>
于 2009-04-22T18:21:35.577 回答