wpf学习心得

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>

之后就可以在其他文件中使用此样式了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值