新手学习wpf button中篇实例学习 等级4

这篇博客主要针对新手,深入探讨了WPF中Button的进阶使用,包括MainWindow.xaml和App.xaml的配置,实现点击按钮改变字体大小的代码,以及创建圆角按钮的详细步骤。此外,还介绍了如何新建并使用BindingData.cs类进行数据绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

button进阶

1.等级3的时候学习了用Grid(网格)布局排列。这次是button背景的引用
学习圆角button。我发现学代码难的原因就是英语不好不懂什么意思。
设置focysable属性为false后button就不会在按下的时候一闪一闪的
了.

运行效果图

MainWindow.xaml代码

<Window x:Class="学习wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="800" >         <!--title标题-->
    <Grid  >     <!--grid 网格格子-->
        <Grid.ColumnDefinitions>     <!--column   列   definitions 定义-->

            <ColumnDefinition Width="33*" />
            <ColumnDefinition Width="33*" />
            <ColumnDefinition Width="33*" />
            <ColumnDefinition Width="Auto"/>       <!--auto自动-->
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>          <!--row 行-->
            <RowDefinition Height="33*" />
            <RowDefinition Height="33*" />
            <RowDefinition Height="33*" />
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
                             <!--content 内容-->  
            <!--background 背景-->
        <Button Content="按钮1:资源" Grid.Column="0" Grid.Row="0" Name="button1"  Background="{DynamicResource HuaShuaZiyuan}"/>
        <Button Content="按钮2:样式" Grid.Column="2" Grid.Row="0"   Name="button2" Style="{StaticResource 样式}" Focusable="False" />   <!--这里设置focusable属性为false后button就不会在按下的时候一闪一闪的了-->
        <Button Content="按钮3颜色变化 "      Grid.Column="1"  Grid.Row="1" Name="button3" >
            <Button.Background>          <!--anim动物;动画    brush刷子;画笔-->
                <!--solid 实在的;固体;立方体     color 颜色-->
                <SolidColorBrush x:Name="AnimBrush"/>
            </Button.Background>
            <Button.Triggers><!--triggers 触发;引起(trigger的单三形式)-->
                             <!--event 事件 routed已选择路径(route的过去分词和过去式)RoutedEvent事件接口-->
                <EventTrigger RoutedEvent="Button.Loaded">
                    <BeginStoryboard><!--begin开始;首先   storyboard情节串连图板-->
                        <Storyboard><!--animation活泼,生气;激励;卡通片绘制-->
                            <!--coloranimation颜色绘制    Storyboard.TargetName动画实例的名字-->
                            <!--Storyboard.TargetProperty连接的绘制的性质是-->
                            <!--from ...to 从...到..   -->
                            <!--duratuib持续时间-->
                            <ColorAnimation Storyboard.TargetName="AnimBrush" 

                                            Storyboard.TargetProperty="(SolidColorBrush.Color)" 
                                            From="Red" To="Green" Duration="0:0:10" 
                                            AutoReverse="True" RepeatBehavior="Forever"/>
                           <!--auto自动 reverse倒转    repeat重复      behavior行为    forever永远-->
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
        </Button>
        <!--binding绑定    source 来源    resource资源   static静态的  data数据  path路径  span跨越-->
        <Button Content="按钮4:Bingding" Grid.Column="1" Grid.Row="2"    Name="button4" Background="{Binding Source={StaticResource myDataSource},Path=ColorName}" Grid.ColumnSpan="2"  />
        <Button Content="圆角按钮" Grid.Column="0" Grid.Row="2"  Name="button5"  Margin="10" Click="button5_Click">
            <Button.Template>        <!--template模板   control控制;管理-->
                <ControlTemplate >
                    <!--border边界   thickness厚度  corner角落;拐角 radius半径-->
                    <Border BorderThickness="1" CornerRadius="3,136,3,136" >
                        <Border.Background >
                            <!--linear直线的;线性的  gradient倾斜的  brush刷子;画笔-->
                            <!--LinearGradientBrush线性渐变-->       <!--end最后  start开始  point指向-->
                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                <!--stop停止  offset偏移-->
                            <GradientStop Color="LightBlue" Offset="0.0" />
                            <GradientStop Color="#ff0082bd" Offset="0.5" />
                            <GradientStop Color="Blue" Offset="0.0" />
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>
        <!--vertical垂直的Alignment对齐  top顶部 margin外边距  click点击   FontSize字体大小-->
        <Button Content="字体大小" Grid.Column="1" Grid.Row="1"  VerticalAlignment="Top"  Margin="20" Name="button6" Click="FontSize_Click" />
    </Grid>
</Window>

App.xaml代码

<Application x:Class="学习wpf.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

           xmlns:local="clr-namespace:学习wpf"  

    StartupUri="MainWindow.xaml">
    <Application.Resources>
              <!--application 应用   resources资源-->
        <!--使用资源引用-->
        <SolidColorBrush x:Key="HuaShuaZiyuan" Color="Pink"/>
        <!--使用样式-->
        <Style x:Key="样式">     <!--style设计-->                 <!--value-->
            <!--setter调节器;设值函数   property性质 control控制管理-->
            <Setter Property="Control.Background" Value="LightBlue"/>
        </Style>
        <!--使用数据绑定-->
        <!--local局部;当地 的   binding绑定   data数据     source来源-->
        <!--出现local缺引用在上面添加  xmlns:local="clr-namespace:你的项目名字xxx"-->
        <local:BindingData x:Key="myDataSource"/>
    </Application.Resources>
</Application>

点击后字体大小button的代码

button5是圆角button代码

   private void FontSize_Click(object sender, RoutedEventArgs e)
        {
                //private 私有的
               //void 空集
            //object  对象;目标
           // sender 发送者
         //   Routed已选择路径  
            //Event事件,结果
            //arg 自变量

            if (FontSize == 12)
            {
                FontSize = 16;
            }
            else
            {
                FontSize=12;
            }
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
             // message消息 
                //box盒子
                //Show 显示
            MessageBox.Show("圆角button 更改主要属性就是CornerRadius ");

        }

新建一个BindingData.cs的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 学习wpf
{
 public   class BindingData
    {
                    //用于按钮4
            public BindingData()                                                             
            {
                ColorName = "Red";
            }
            private string name = "Red";
            public string ColorName
            {
                get { return name; }
                set { name = value; }
            }
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值