wpf中Grid的比例划分
1.绝对
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition />
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
2.按比例
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
此代码中有三行,第一行占1/6,第二行占2/6,第三行占3/6。
3.自动填充
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="AUTO"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
若第一行有元素,则展示元素的大小,如若没有,则不展示此行。
wpf中style的设计
1.直接在单个标签中设置样式
<Button Background="Red" FontSize="20" Height="20" Width="30"/>
2.利用style标签设置样式
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Blue"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="100"/>
</Style>
<Style x:Key="a" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Green"/>
<Setter Property="FontSize" Value="10"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="100"/>
</Style>
<Style x:Key="b" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Red"/>
<Setter Property="FontSize" Value="10"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="100"/>
</Style>
</Window.Resources>
<StackPanel>
<Button Style="{StaticResource a}" Content="登录"/>
<Button Style="{StaticResource b}" Content="退出"/>
<Button Content="忘记密码"/>
<Button Content="重置密码"/>
</StackPanel>
重点:特殊样式怎么继承基础样式。元素引用样式的方法。
3.在资源字典xaml文件中设置样式,在app.xaml文件中将此文件导入,使的全部的文件都可以引用此样式
新建资源字典xaml文件
资源字典xaml文件中设置style
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="Background" Value="Blue"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="100"/>
</Style>
<Style x:Key="a" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Green"/>
<Setter Property="FontSize" Value="10"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="100"/>
</Style>
<Style x:Key="b" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Red"/>
<Setter Property="FontSize" Value="10"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Width" Value="100"/>
</Style>
</ResourceDictionary>
在app.xaml将次资源文件导入
<Application x:Class="WpfApp2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfApp2;component/basebuttonstyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
之后就可以在其他文件中使用此样式了。