Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

スタイルの継承

アプリ実行


ソースコード

App.xaml
<Application x:Class="WpfApplication7.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
</Application>
MainWindow.xaml
<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="150">
    <Window.Resources>
        <!-- 基本スタイル -->
        <Style x:Key="baseControls" TargetType="{x:Type Control}">
            <Setter Property="Background" Value="Aqua" />
        </Style>

        <!-- 基本スタイルを継承してボタンに適用 -->
        <Style
            x:Key="{x:Type Button}"
            TargetType="{x:Type Button}"
            BasedOn="{StaticResource baseControls}">

            <Setter Property="Foreground" Value="Red" />
        </Style>

        <!-- 基本スタイルを継承してチェックボックスに適用 -->
        <Style
            x:Key="{x:Type CheckBox}"
            TargetType="{x:Type CheckBox}"
            BasedOn="{StaticResource baseControls}">

            <Setter Property="Foreground" Value="Green" />
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button>Hello World</Button>
        <CheckBox>Hello World</CheckBox>
    </StackPanel>
</Window>