zoukankan      html  css  js  c++  java
  • wpf之样式

     看下面代码:

    <Window x:Class="MyWpf.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:MyWpf"
            mc:Ignorable="d" 
            Title="MainWindow" Height="250" Width="500">
        <Grid  >
            <Button Content="Button" Foreground="Red" HorizontalAlignment="Left" Margin="223,20,0,0" VerticalAlignment="Top" Width="75"/>
            <Button Content="Button"  Foreground="Red" HorizontalAlignment="Left" Margin="223,65,0,0" VerticalAlignment="Top" Width="75"/>
            <Button Content="Button"   Foreground="Red" HorizontalAlignment="Left" Margin="223,110,0,0" VerticalAlignment="Top" Width="75"/>
           
        </Grid>
    </Window>

    界面:

    上面代码中Button标签中属性的值都是一样的,重复的,所以我们可以用下面方法来做更改,下面还展示了继承的用法:

    <Window x:Class="MyWpf.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:MyWpf"
            mc:Ignorable="d" 
            Title="MainWindow" Height="250" Width="500">
        <Window.Resources>
            <!--表示这个样式只针对于Button标签,这个样式的key值是BaseStyle,
            -->
            <Style TargetType="Button" x:Key="BaseStyle">
                <Setter Property="Foreground" Value="Red"></Setter>
                <Setter Property="HorizontalAlignment" Value="Left"></Setter>
                <Setter Property="VerticalAlignment" Value="Top"></Setter>
                <Setter Property="Width" Value="75"></Setter>
            </Style>
            <!--表示这个样式继承于BaseStyle-->
            <Style x:Key="style1" TargetType="Button" BasedOn="{StaticResource ResourceKey=BaseStyle}" >
                <Setter Property="Content" Value="hello"></Setter>
            </Style>
        </Window.Resources> 
        <Grid>
            <Button Style="{StaticResource ResourceKey=style1}"   Margin="223,20,0,0"   />
            <Button Style="{StaticResource ResourceKey=style1}"  Margin="223,65,0,0" />
            <Button Style="{StaticResource ResourceKey=style1}"   Margin="223,110,0,0"  />
    
        </Grid>
    </Window>
  • 相关阅读:
    [Linux] 设置系统时区
    [Python] 当猎头遇上 Guido van Rossum
    [Ubuntu] LightDM 轻量级桌面显示管理器
    [Java] Apache Ant 构建基础教程
    [Python] pip 简明指南
    .NET Core下的Spring Cloud——前言和概述
    福州首届.NET开源社区技术交流会圆满成功
    【福州活动】| "福州首届.NET开源社区线下技术交流会"(2018.11.10)
    使用CoreRT将.NET Core发布为Native应用程序
    使用.NET Core快速开发一个较正规的命令行应用程序
  • 原文地址:https://www.cnblogs.com/anjingdian/p/15456174.html
Copyright © 2011-2022 走看看