Visual Studio / WPF > Resources / Style > x:Keyを指定しない Implicit keyの例


動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

XAML Resources

Styles, DataTemplates, and Implicit Keys
...
The implicit key from the standpoint of requesting it is the Type of the control itself. The implicit key from the standpoint of defining the resource is the TargetType of the style.

Styleに関して、x:Keyを指定せずにTargetTypeで定義しておけばStyleを変更できるとのこと。

MainWindow.xaml
<Window x:Class="_170615_t1450_ImplicitKey.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170615_t1450_ImplicitKey"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush>
                        <GradientStop Offset="0.0" Color="AliceBlue"/>
                        <GradientStop Offset="1.0" Color="Salmon"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="FontSize" Value="18"/>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Width="100" Height="28" Content="Button1"/>
            <Button Width="100" Height="28" Content="Button2"/>
            <Button Width="100" Height="28" Content="Button3"/>
        </StackPanel>
    </Grid>
</Window>

関連リンク

スタイルの暗黙的な適用