Wpf-layout-level

提供:Dev Guides
移動先:案内検索

WPF-レイアウトレベル

レイアウトレベルでスタイルを定義すると、そのレイアウトとその子要素のみがスタイルにアクセスできるようになります。 以下に示すのは、3つのボタンすべてに共通のスタイルがあるレイアウトレベルの例です。

<Window x:Class = "XAMLLayoutLevelStyle.MainWindow"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   Title = "MainWindow" Height = "350" Width = "604">

   <StackPanel Margin = "10">
      <StackPanel.Resources>
         <Style TargetType = "Button">
            <Setter Property = "Foreground" Value = "Blue"/>
            <Setter Property = "FontStyle" Value = "Italic"/>
            <Setter Property = "Width" Value = "100"/>
            <Setter Property = "Height" Value = "40"/>
            <Setter Property = "Margin" Value = "10"/>
         </Style>
      </StackPanel.Resources>

      <Button>Button 1</Button>
      <Button>Button 2</Button>
      <Button Foreground = "Blue">Button 3</Button>
   </StackPanel>

</Window>

上記のコードをコンパイルして実行すると、次のウィンドウが生成されます。

レイアウトレベル