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 に答える