WPF - 应用程序级别

在应用程序级别定义样式可以使其在整个应用程序中可访问。 让我们采用相同的示例,但在这里,我们将把样式放入 app.xaml 文件中,以便在整个应用程序中都可以访问它。 以下是 app.xaml 中的 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>

以下是用于创建文本块和文本框的 XAML 代码。

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

当您编译并执行上述代码时,将产生以下窗口。

应用程序级别

我们建议您执行上述代码并尝试向其中插入更多功能。

❮ wpf_styles.html