首页
学习
活动
专区
圈层
工具
发布
50 篇文章
1
【愚公系列】2023年09月 WPF控件专题 XAML介绍
2
【愚公系列】2023年09月 WPF控件专题 WPF应用程序组成
3
【愚公系列】2023年09月 WPF控件专题 Window窗体属性和事件
4
【愚公系列】2023年09月 WPF控件专题 Label、TextBox、PasswordBox控件介绍
5
【愚公系列】2023年09月 WPF控件专题 Button控件详解
6
【愚公系列】2023年09月 WPF控件专题 RadioButton控件详解
7
【愚公系列】2023年09月 WPF控件专题 CheckBox控件详解
8
【愚公系列】2023年09月 WPF控件专题 Image控件详解
9
【愚公系列】2023年09月 WPF控件专题 Border控件详解
10
【愚公系列】2023年09月 WPF控件专题 ComboBox控件详解
11
【愚公系列】2023年09月 WPF控件专题 ListBox控件详解
12
【愚公系列】2023年09月 WPF控件专题 DatePicker控件详解
13
【愚公系列】2023年09月 WPF控件专题 Calendar控件详解
14
【愚公系列】2023年09月 WPF控件专题 Slider控件详解
15
【愚公系列】2023年09月 WPF控件专题 ProgressBar控件详解
16
【愚公系列】2023年10月 WPF控件专题 StackPanel控件详解
17
【愚公系列】2023年10月 WPF控件专题 WrapPanel控件详解
18
【愚公系列】2023年10月 WPF控件专题 DockPanel控件详解
19
【愚公系列】2023年10月 WPF控件专题 Canvas控件详解
20
【愚公系列】2023年10月 WPF控件专题 Grid控件详解
21
【愚公系列】2023年10月 WPF控件专题 Groupbox控件详解
22
【愚公系列】2023年10月 WPF控件专题 Expander控件详解
23
【愚公系列】2023年10月 WPF控件专题 TabControl控件详解
24
【愚公系列】2023年10月 WPF控件专题 Frame控件详解
25
【愚公系列】2023年10月 WPF控件专题 ListView控件详解
26
【愚公系列】2023年10月 WPF控件专题 DataGrid控件详解
27
【愚公系列】2023年10月 WPF控件专题 Menu控件详解
28
【愚公系列】2023年10月 WPF控件专题 ContextMenu控件详解
29
【愚公系列】2023年10月 WPF控件专题 TreeView控件详解
30
【愚公系列】2023年10月 WPF控件专题 ToolBar控件详解
31
【愚公系列】2023年10月 WPF控件专题 ToolBarTray控件详解
32
【愚公系列】2023年10月 WPF控件专题 StatusBar控件详解
33
【愚公系列】2023年11月 WPF控件专题 MediaElement控件详解
34
【愚公系列】2023年11月 WPF控件专题 RichTextBox控件详解
35
【愚公系列】2023年11月 WPF控件专题 GridView控件详解
36
【愚公系列】2023年11月 WPF控件专题 Rectangle控件详解
37
【愚公系列】2023年11月 WPF控件专题 Popup控件详解
38
【愚公系列】2023年11月 WPF控件专题 OpenFileDialog控件详解
39
【愚公系列】2023年11月 WPF控件专题 SaveFileDialog控件详解
40
【愚公系列】2023年11月 WPF控件专题 RepeatButton控件详解
41
【愚公系列】2023年11月 WPF控件专题 Ellipse控件详解
42
【愚公系列】2023年11月 WPF控件专题 Polygon控件详解
43
【愚公系列】2023年11月 WPF控件专题 Path控件详解
44
【愚公系列】2023年11月 WPF控件专题 WindowFormsHost控件详解
45
【愚公系列】2023年11月 WPF控件专题 WebBrowser控件详解
46
【愚公系列】2023年11月 WPF控件专题 Validation控件详解
47
【愚公系列】2023年11月 WPF控件专题 Page控件详解
48
【愚公系列】2023年11月 WPF控件专题 PrintDialog控件详解
49
【愚公系列】2023年11月 WPF控件专题 Track控件详解
50
【愚公系列】2023年11月 WPF控件专题 Polyline控件详解

【愚公系列】2023年09月 WPF控件专题 Button控件详解

🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。 🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。 🏆🎉欢迎 👍点赞✍评论⭐收藏

前言

WPF控件是Windows Presentation Foundation(WPF)中的基本用户界面元素。它们是可视化对象,可以用来创建各种用户界面。WPF控件可以分为两类:原生控件和自定义控件。

原生控件是由Microsoft提供的内置控件,如Button、TextBox、Label、ComboBox等。这些控件都是WPF中常见的标准用户界面元素。

自定义控件则允许开发人员使用XAML和C#等编程语言来创建个性化的用户界面元素。自定义控件可以根据需求提供更多的功能和自定义化选项,以及更好的用户体验。

一、Button控件详解

WPF中的Button控件可以提供用户交互性,它可以作为命令控件或触发器控件的触发器使用。下面是Button控件的一些属性和常用场景:

1.属性介绍

  • Content:Button控件上显示的文本或图像。
  • Command:与Button关联的命令,当Button被点击时触发该命令。
  • CommandParameter:向关联的命令传递的参数。
  • IsEnabled:Button控件是否启用。
  • IsDefault:指定该Button是否作为默认Button。
  • IsCancel:指定该Button是否作为取消Button。
  • ClickMode:指定Button被单击后应该如何响应,比如点击即触发点击事件,按下鼠标时触发点击事件,等等。
  • Style:指定Button控件的样式。

2.常用场景

  • 执行命令:将Button控件与一个命令关联,当Button被单击时,该命令将被执行。
  • 表单提交:使用Button控件作为提交按钮,以提交表单数据。
  • 窗口操作:使用Button控件作为关闭窗口、最小化窗口等操作的触发器。

下面是一些Button控件的案例:

  1. 普通Button
代码语言:html
复制
<Button Content="Click Me" Click="Button_Click"/>
  1. 与Command关联的Button
代码语言:html
复制
<Button Content="Save" Command="{Binding SaveCommand}"/>
  1. 带参数的Command
代码语言:html
复制
<Button Content="Delete" Command="{Binding DeleteCommand}" 
        CommandParameter="{Binding SelectedItem}"/>
  1. 带样式的Button
代码语言:html
复制
<Button Content="OK" Style="{StaticResource Button_Style}"/>
  1. 默认Button和取消Button
代码语言:html
复制
<StackPanel>
    <Button Content="OK" IsDefault="True"/>
    <Button Content="Cancel" IsCancel="True"/>
</StackPanel>
  1. 自定义模板的Button
代码语言:html
复制
<Button Content="Click Me">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Grid>
                <Ellipse Fill="Orange"/>
                <ContentPresenter HorizontalAlignment="Center" 
                                  VerticalAlignment="Center"/>
            </Grid>
        </ControlTemplate>
    </Button.Template>
</Button>

3.具体案例

以下是一个简单的WPF登录界面的代码示例:

XAML代码:

代码语言:html
复制
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Login" Height="300" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock Text="Username:" Grid.Row="0" Margin="10"/>
        <TextBox Name="UsernameTextBox" Grid.Row="0" Margin="100,0,10,0"/>
        <TextBlock Text="Password:" Grid.Row="1" Margin="10"/>
        <PasswordBox Name="PasswordBox" Grid.Row="1" Margin="100,0,10,0"/>
        <Button Name="LoginButton" Grid.Row="2" Margin="10" Content="Login" Click="LoginButton_Click" />
        <TextBlock Name="ErrorMessage" Text="" Foreground="Red" Grid.Row="3" Margin="10"/>
    </Grid>
</Window>

C#代码:

代码语言:c#
复制
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void LoginButton_Click(object sender, RoutedEventArgs e)
    {
        string username = UsernameTextBox.Text;
        string password = PasswordBox.Password;

        // 检查用户名和密码是否正确
        if (IsValidUser(username, password))
        {
            // 登录成功,显示欢迎信息
            ErrorMessage.Text = "Welcome, " + username + "!";
        }
        else
        {
            // 登录失败,显示错误信息
            ErrorMessage.Text = "Invalid username or password.";
        }
    }

    private bool IsValidUser(string username, string password)
    {
        // 在实际应用中,这里应该是根据用户名和密码去数据库或者其他存储中查询用户信息是否存在的代码
        return (username == "admin" && password == "password");
    }
}

这个登录界面包括一个用户名输入框,一个密码输入框和一个登录按钮。当用户点击登录按钮时,程序将获取用户输入的用户名和密码,并调用 IsValidUser 方法检查它们是否有效。如果有效,则显示欢迎信息;否则,显示错误信息。在本例中,我们只是简单地比较用户名和密码是否是 "admin" 和 "password"。在实际应用中,您应该使用更安全的方法来验证用户。


我正在参与2023腾讯技术创作特训营第二期有奖征文,瓜分万元奖池和键盘手表

下一篇
举报
领券