方法一:
1.在.xaml中添加
<Button Click="Button_Click"><Button>2.在.cs中添加
private void Button_Click(object sender, RoutedEventArgs e)
{...//触发事件}方法二:
在.xaml中添加Button元素,在视图中双击Button元素,编译器自动跳转至.cs界面,方法一中的代码被自动添加
xaml代码
<Window x:Class="ButtonClick.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:ButtonClick"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="0,0,293,145">
<Button Margin="168,149,-82,-16" Click="Button_Click">点击触发事件</Button>
</Grid>
</Window>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ButtonClick
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("点击了click事件");
}
}
}
点击前:
点击后:
本文介绍了两种在WPF中实现按钮点击触发事件的方法。方法一是手动在.xaml和.cs文件中添加相关代码;方法二是通过在视图中双击Button元素,让编译器自动生成事件处理代码。
4047

被折叠的 条评论
为什么被折叠?



