Xaml继承的样式
问题描述:
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>
答
没有办法做到这一点。但是,您可以定义基本样式并从中继承:
<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>