首页 > 其他分享 >wpf之样式

wpf之样式

时间:2023-11-01 23:33:36浏览次数:36  
标签:样式 button 单独 设置 按钮 wpf

在Window.Resources中书写样式 ;

<Window.Resources>
<Style TargetType="Button" >

</Style>
</Window.Resources>
TargetType 是指定标签的类型 ;
<Style TargetType="Button" >

</Style>中的style样式会在所有的button按钮中生效 ;

 <Window.Resources>
        <Style TargetType="Button" >
            <Setter Property="FontSize" Value="18"></Setter>
            <Setter Property="Content" Value="BUTTON"></Setter>
            <Setter Property="Background" Value="red"></Setter>
            <!-- 字体 -->
            <Setter Property="Foreground" Value="white"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button />
            <Button />
            <Button />
            <Button />
        </StackPanel>
    </Grid>

x:key 可以给一个Style一个唯一的标识(类名)可以单独设置某一个button的样式,而不是现在给所有的button都设置了样式 ;

比如:

 <Window.Resources>
        <Style x:Key="ButtonStyle" TargetType="Button" >
            <Setter Property="FontSize" Value="18"></Setter>
            <Setter Property="Content" Value="BUTTON"></Setter>
            <Setter Property="Background" Value="red"></Setter>
            <!-- 字体 -->
            <Setter Property="Foreground" Value="white"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Style="{StaticResource ButtonStyle}" />
            <Button />
            <Button   Style="{StaticResource ButtonStyle}" />
            <Button />
        </StackPanel>
    </Grid>

 样式:静态资源

 

如果想要从这些样式提取公共的样式复用,一些样式单独使用:比如给四个按钮不同的字体颜色 ;

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="CommonStyle" TargetType="Button">
             <Setter Property="FontSize" Value="18"></Setter>
            <Setter Property="Content" Value="BUTTON"></Setter>
            <Setter Property="Background" Value="red"></Setter>
        </Style>
           
        <Style x:Key="ButtonStyle1" TargetType="Button" BasedOn="{StaticResource CommonStyle}" >
            <Setter Property="Foreground" Value="white"></Setter>
        </Style>
        <Style x:Key="ButtonStyle2" TargetType="Button" BasedOn="{StaticResource CommonStyle}" >
            <Setter Property="Foreground" Value="lime"></Setter>
        </Style>
        <Style x:Key="ButtonStyle3" TargetType="Button" BasedOn="{StaticResource CommonStyle}" >
            <Setter Property="Foreground" Value="blue"></Setter>
        </Style>
        <Style x:Key="ButtonStyle4" TargetType="Button" BasedOn="{StaticResource CommonStyle}" >
            <Setter Property="Foreground" Value="yellow"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Style="{StaticResource ButtonStyle1}" />
            <Button Style="{StaticResource ButtonStyle2}" />
            <Button   Style="{StaticResource ButtonStyle3}" />
            <Button Style="{StaticResource ButtonStyle4}" />
        </StackPanel>
    </Grid>
</Window>

 

标签:样式,button,单独,设置,按钮,wpf
From: https://www.cnblogs.com/zhulongxu/p/17804414.html

相关文章

  • tailwindcss 实现常见样式
    设置宽高w-number,h-numberflex实现水平垂直居中flex设置容器为flex容器flex-row/flex-column设置主轴方向justify-center设置主轴方向居中,类似还有justify-start,justify-enditems-center设置交叉轴方向居中,类似还有items-start,items-end画borderborde......
  • Vue动态添加style样式
    最近在用uniapp开发安卓app,由于语法跟vue一致,就梳理了下动态添加style的方法:Object :style="{fontSize:fontSize+'px'}":style="{fontSize:(fontSize?fontSize:'12')+'px'}" Array :style="[baseStyles,otherStyle......
  • less变量书写及样式混入
    定义变量定义混入样式变量及混入样式使用样式文件中  ~@代表src ......
  • [17章+电子书]C#速成指南-从入门到进阶,实战WPF与Unity3D开发
    点击下载:[17章+电子书]C#速成指南-从入门到进阶,实战WPF与Unity3D开发  提取码:a3s5 《C#速成指南--从入门到进阶,实战WPF与Unity3D开发》完整讲解了C#语言的核心知识和高阶编程技巧,并结合WPF客户管理系统和Unity3D切水果游戏两大实战项目,帮你实现技术的精通,完成从Zero到Hero的蜕变......
  • java poi 导出excel 单元格样式
    CellStylecs=wb.createCellStyle();//设置字体Fontfont=wb.createFont();font.setColor(IndexedColors.BLACK.getIndex());font.setFontHeightInPoints((short)12);font.setBold(true);//字体样式cs.setFont(font1);//边框cs.setBorderLeft(BorderStyle.THIN);......
  • CSS样式之基础选择器
    CSS样式css样式的作用是改变标签的内容如何选中标签的内容? 方法是使用选择器来实现①标签选择器 ②类选择器 ③id选择器④通配符选择器 标签选择器语法:标签{属性:值;属性:值;.........属性:值;}举个例子:1<!DOCTYPEhtml>2<htmllang="en">34<head>......
  • 界面控件DevExpress WPF Gauge组件 - 轻松实现个性化商业仪表盘
    DevExpressWPFGauge(仪表)控件包含了多种圆形仪表类型、水平和垂直线性仪表、分段和矩阵数字仪表以及状态指示器,同时还具有最终用户交互性的集成支持。P.S:DevExpressWPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着......
  • Qt中设置设置控件样式 — setStyleSheet
    通过setStyleSheet来设置控件的Style样式控件样式的常用设置项:1、border:设置控件的边框样式//1.无边框QStringborderNone="border:none";//2.设置边框的值需要有3个因子:宽度,线形,颜色QStringborderStyle="border:5pxsolid#999999;";......
  • wpf prism 自定义委托命令DelegateCommand
    //构造函数publicMainWindowViewModel(){EditCommand=newDelegateCommand(_editCommand);}//命令声明一publicDelegateCommandEditCommand{get;set;}//构造函数中实例化委托void_editCommand(){MessageBox.Show("EditCommand");}//命令生命......
  • xsl样式表学习
    XSL指扩展样式表语言(ExtensibleStylesheetLanguage)万维网联盟开始发展样式表的起因是由于对基于XML的样式表语言的需求XSLT指XSL转换。XSLT可以将XML文档转换为其他文档,比如XHTMLXSLT参考手册XSLT元素https://www.w3school.com.cn/xsl/xsl_w3celementref.asp对所有来自W3C......