下载Visual Studio
新建WPF模板
在搜索框输入“WPF”,选择“WPF应用(.NET Framework)”
创建成功,MainWindow.xaml是UI界面,在这个界面设计控件的属性,在MainWindow.xaml.cs后台文件中也可以对UI界面控件的属性进行修改。MainWindow.xaml.cs文件是后台用来实现逻辑的,比如说,在UI界面定义了按钮Button的点击(Click)功能属性,此时点击按钮就不会触发任何事件,如果在MainWindow.xaml.cs文件中对点击(Click)事件实现了点击按钮弹出一个对话框,那么你点击按钮之后就会弹出一个对话框。
实现:按下按钮,弹出一个对话框,对话框显示“This is my first WpfApp”
1、打开工具箱,找到Button控件,拖拽至UI界面
2、在XAML文件中就会出现<Button / >标签,可在里面修改Button的属性
3、在Button标签中添加点击事件Click = Button_Click,在下方会弹出一个“<新建事件处理程序>”,这是检测到在MainWindow.xaml.cs文件中未找到该方法,双击“<新建事件处理程序>”,cs文件中就会自动创建该方法。
4、在Button_Clicko方法中定义实现的逻辑
MessageBox.Show("This is my first WpfApp");
完整的代码
//MainWindow.xaml
<Window x:Class="FirstWPF.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:FirstWPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="100,70,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
//MainWindow.xaml.cs
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 FirstWPF
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("This is my first WpfApp");
}
}
}
【运行结果】:
实现:在上一个要求的基础上,新建一个Label控件,用鼠标右键点击按钮获取本机时间并通过Label控件显示
1、找到label控件,拖拽至你想放的位置,可通过拉伸来修改label控件大小为你心仪的大小,也可在XAML中修改Label控件的属性,如Content = “”,修改Label显示的文字为“”(为空),值得说的一点是,如果想在cs后台文件中修改Label控件的属性,就要在XAML文件定义Label控件的Name属性。
2、在XAML中向Button添加一个右键点击事件,然后在cs文件中实现右键点击的逻辑。
//MainWindow.xaml
<Button x:Name="Button" Content="Button" HorizontalAlignment="Left" Margin="300,150,0,0" VerticalAlignment="Top" Width="235" Click="Button_Click" MouseRightButtonDown="Button_RightButtonDown"/>
//MainWindow.xaml.cs
private void Button_RightButtonDown(object sender, MouseButtonEventArgs e)
{
string str = "本地时间:";
DateTime currentTime = DateTime.Now;
string formattedTime = str + currentTime.ToString("yyyy-MM-dd HH:mm:ss");
ShowTime.Content = formattedTime;
}
完整代码
//MainWindow.xaml.cs
<Window x:Class="FirstWPF.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:FirstWPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="Button" Content="Button" HorizontalAlignment="Left" Margin="300,150,0,0" VerticalAlignment="Top" Width="235" Click="Button_Click" MouseRightButtonDown="Button_RightButtonDown"/>
<Label x:Name="ShowTime" Content="" HorizontalAlignment="Left" Margin="300,105,0,0" VerticalAlignment="Top" Width="235"/>
//MainWindow.xaml.cs
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 FirstWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("This is my first WpfApp");
}
private void Button_RightButtonDown(object sender, MouseButtonEventArgs e)
{
string str = "本地时间:";
DateTime currentTime = DateTime.Now;
string formattedTime = str + currentTime.ToString("yyyy-MM-dd HH:mm:ss");
ShowTime.Content = formattedTime;
}
}
}