WPF - Control Level



Defining a style on control level can only be applied to that particular control. Given below is an example of a control level where the button and TextBlock have their own style.

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

When the above code is compiled and executed, it will produce the following window −

Control Level
wpf_styles.htm
Advertisements