- XAML 教程
- XAML - 主页
- XAML - 概述
- XAML - 环境设置
- 在 MAC OS 上编写 XAML 应用程序
- XAML 与 C# 代码
- XAML 与 VB.NET
- XAML - 构建模块
- XAML - 控件
- XAML - 布局
- XAML - 事件处理
- XAML - 数据绑定
- XAML - 标记扩展
- XAML - 依赖项属性
- XAML - 资源
- XAML - 模板
- XAML - 样式
- XAML - 触发器
- XAML - 调试
- XAML - 自定义控件
- XAML 实用资源
- XAML - 快速指南
- XAML - 实用资源
- XAML - 讨论
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">
<Window.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>
</Window.Resources>
<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>
编译并执行上述代码后,将生成以下输出 −
xaml_styles.htm
广告