作者:霸气的萱---_299 | 来源:互联网 | 2023-10-13 13:26
button进阶1.等级3的时候学习了用Grid(网格)布局排列。这次是button背景的引用学习圆角button。我发现学代码难的原因就是英语不好不懂什么意思。设置focysable属性为
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" >
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*" />
<ColumnDefinition Width="33*" />
<ColumnDefinition Width="33*" />
<ColumnDefinition Width="Auto"/>
Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="33*" />
<RowDefinition Height="33*" />
<RowDefinition Height="33*" />
<RowDefinition Height="Auto"/>
Grid.RowDefinitions>
<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" />
<Button Content="按钮3颜色变化 " Grid.Column="1" Grid.Row="1" Name="button3" >
<Button.Background>
<SolidColorBrush x:Name="AnimBrush"/>
Button.Background>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Loaded">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="AnimBrush"
Storyboard.TargetProperty="(SolidColorBrush.Color)"
From="Red" To="Green" Duration="0:0:10"
AutoReverse="True" RepeatBehavior="Forever"/>
Storyboard>
BeginStoryboard>
EventTrigger>
Button.Triggers>
Button>
<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>
<ControlTemplate >
<Border BorderThickness="1" CornerRadius="3,136,3,136" >
<Border.Background >
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<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>
<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>
<SolidColorBrush x:Key="HuaShuaZiyuan" Color="Pink"/>
<Style x:Key="样式"> --style设计--> --value值-->
--setter调节器;设值函数 property性质 control控制管理-->
<Setter Property="Control.Background" Value="LightBlue"/>
Style>
<local:BindingData x:Key="myDataSource"/>
Application.Resources>
Application>
private void FontSize_Click(object sender, RoutedEventArgs e)
{
if (FOntSize== 12)
{
FOntSize= 16;
}
else
{
FOntSize=12;
}
}
private void button5_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("圆角button 更改主要属性就是CornerRadius ");
}
新建一个BindingData.cs的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 学习wpf
{
public class BindingData
{
public BindingData()
{
ColorName = "Red";
}
private string name = "Red";
public string ColorName
{
get { return name; }
set { name = value; }
}
}
}