XAML - 控制元件級別



在控制元件級別中定義樣式僅適用於特定的控制元件。下面是控制元件級別的示例,其中 Button 和 TextBlock 都有自己的樣式。

<Window x:Class = "XAMLControlLevelStyle.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "Control Level Styles" Height = "350" Width = "604">
	
   <StackPanel Margin = "10" VerticalAlignment = "Top"> 
      <TextBlock Text = "TextBlock">
         <TextBlock.Style> 
            <Style> 
               <Setter Property = "TextBlock.FontSize" Value = "24" />
               <Setter Property = "TextBlock.Width" Value = "400" />
               <Setter Property = "TextBlock.Height" Value = "40" />
               <Setter Property = "TextBlock.Background" Value = "Gray" />
               <Setter Property = "TextBlock.Margin" Value = "50" />
            </Style> 
         </TextBlock.Style>
      </TextBlock>
		
      <Button Content = "Button">
         <Button.Style> 
            <Style> 
               <Setter Property = "TextBlock.Width" Value = "100" />
               <Setter Property = "TextBlock.Height" Value = "40" />
               <Setter Property = "TextBlock.Margin" Value = "50" />
            </Style>
         </Button.Style> 
      </Button>
   </StackPanel>

</Window>

編譯並執行以上程式碼後,將生成以下輸出 −

Control Level
xaml_styles.htm
廣告
© . All rights reserved.