样式的定义和引用方式
一:前台界面中直接定义
如常见的窗体的样式定义:
- Title:定义窗体的标题
- Height:高度
- Width:宽度
- WindowStyle:窗体样式
- AllowsTransparency:全透明
- WindowStartupLocation:开始出现位置
- ResizeMode:大小调整
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfTest"
mc:Ignorable="d"
Title="MainWindow"
Height="450" Width="800"
WindowStyle="None" AllowsTransparency="True"
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize"
>
<Grid>
</Grid>
</Window>
二:引用样式
定义样式资源文件如下图:
样式文件内容如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="WindowStyleone" TargetType="Window">
<Setter Property="Title" Value="MainWindow"/>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Height" Value="450"/>
<Setter Property="Width" Value="800"/>
<Setter Property="ResizeMode" Value="NoResize"/>
</Style>
</ResourceDictionary>
窗体进行引用(最终效果和上面的案例一样):
样式的引用方式还可以通过App.xaml如下图:
该方法的效果和窗体xaml处引用实现效果是一样的,即可代替通过 <Window.Resources>引用的方式!