XAML - 布局级别



在任何布局级别定义样式都只能由该布局及其子元素访问。下面给出了一个布局级别的示例,其中所有三个按钮都有一个公共样式。

<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>

当上面代码编译并执行时,它将产生以下输出 −

Layout Level
xaml_styles.htm
广告