WPF - Niveau d'application

La définition d'un style au niveau de l'application peut le rendre accessible dans toute l'application. Prenons le même exemple, mais ici, nous allons mettre les styles dans le fichier app.xaml pour le rendre accessible dans toute l'application. Voici le code XAML dans app.xaml.

<Application x:Class = "Styles.App" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   StartupUri = "MainWindow.xaml"> 
	
   <Application.Resources> 
      <Style TargetType = "TextBlock"> 
         <Setter Property = "FontSize" Value = "24" /> 
         <Setter Property = "Margin" Value = "5" /> 
         <Setter Property = "FontWeight" Value = "Bold" /> 
      </Style> 
		
      <Style TargetType = "TextBox">
         <Setter Property = "HorizontalAlignment" Value = "Left" /> 
         <Setter Property = "FontSize" Value = "24" /> 
         <Setter Property = "Margin" Value = "5" /> 
         <Setter Property = "Width" Value = "200" /> 
         <Setter Property = "Height" Value="40" /> 
      </Style> 
		
   </Application.Resources>
	
</Application>

Voici le code XAML pour créer des blocs de texte et des zones de texte.

<Window x:Class = "Styles.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">
	
   <Grid> 
      <Grid.RowDefinitions> 
         <RowDefinition Height = "Auto" /> 
         <RowDefinition Height = "Auto" /> 
         <RowDefinition Height = "Auto" /> 
         <RowDefinition Height = "*" /> 
      </Grid.RowDefinitions> 
		
      <Grid.ColumnDefinitions> 
         <ColumnDefinition Width = "*" /> 
         <ColumnDefinition Width = "2*" /> 
      </Grid.ColumnDefinitions> 
		
      <TextBlock Text = "First Name: "/>  
      <TextBox Name = "FirstName" Grid.Column = "1" /> 
      <TextBlock Text = "Last Name: " Grid.Row = "1" /> 
      <TextBox Name = "LastName" Grid.Column = "1" Grid.Row = "1" /> 
      <TextBlock Text = "Email: " Grid.Row = "2" /> 
      <TextBox Name = "Email" Grid.Column = "1" Grid.Row = "2"/> 
   </Grid> 
	
</Window>

Lorsque vous compilez et exécutez le code ci-dessus, il produira la fenêtre suivante.

Nous vous recommandons d'exécuter le code ci-dessus et d'essayer d'y insérer plus de fonctionnalités.