2009-04-22 125 views
4

XAML中是否有方法設置將應用於所有控件的樣式?例如,下面我想對我的所有控件添加一個邊距。我可以通過將TargetType更改爲Button,CheckBox等來爲每種類型添加樣式。相反,我想將它設置爲像我在下面那樣設置從Control繼承的所有類型的樣式:Xaml繼承的樣式

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

回答

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>