WPF入门之hello WPF!

2017-08-20 23:14:42来源:CSDN作者:u010476739人点击

分享

环境:vs2013

示例代码:http://pan.baidu.com/s/1jIgiaYM

1.启动vs2013,创建wpf项目


2.创建后的项目结构


需要注意的是,这个App.xaml是程序的起点,里面“x:Class"表示它编译用的类名,但是不一定非的是App.xaml.cs文件里定义的类,即使随便写一个不存在的类也是可以的。

StartupUri表示定义的主窗体

3.主窗体的代码MaiWindow.xaml

<Window x:Class="Demo1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525">    <Grid>        <Grid.RowDefinitions>            <RowDefinition />            <RowDefinition />            <RowDefinition />            <RowDefinition />        </Grid.RowDefinitions>        <Grid.ColumnDefinitions>            <ColumnDefinition Width="100" />            <ColumnDefinition Width="*"/>        </Grid.ColumnDefinitions>        <Label Grid.ColumnSpan="2" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="宋体" FontSize="35">Hello WPF!</Label>        <Label Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="宋体" FontSize="35">账户:</Label>        <Label Grid.Column="0" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="宋体" FontSize="35">密码:</Label>        <TextBox Name="txtUser" Grid.Column="1" Grid.Row="1" FontFamily="宋体" FontSize="20" Text="请输入账户名称" Tag="请输入账户名称" Foreground="Gray" VerticalAlignment="Center" GotFocus="txtUser_GotFocus" LostFocus="txtPWD_LostFocus"/>        <TextBox Name="txtPWD" Grid.Column="1" Foreground="Gray" Grid.Row="2" FontFamily="宋体" FontSize="20" Text="请输入账户密码" Tag="请输入账户密码" VerticalAlignment="Center" GotFocus="txtUser_GotFocus" LostFocus="txtPWD_LostFocus"/>        <Grid Grid.Row="3" Grid.ColumnSpan="2">            <Grid.ColumnDefinitions>                <ColumnDefinition Width="258" />                <ColumnDefinition />            </Grid.ColumnDefinitions>            <Button Content="登陆" FontFamily="宋体" Margin="74,10,64,21" FontSize="35" Grid.Row="0" Grid.Column="0" Click="Button_Click"></Button>            <Button Content="取消" FontFamily="宋体" FontSize="35" Grid.Row="0" Grid.Column="1" Click="Button_Click_1" Margin="24,8,119,19"></Button>        </Grid>    </Grid></Window>

4.主窗体定义类的代码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 Demo1{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void Button_Click(object sender, RoutedEventArgs e)        {            string user = txtUser.Text;            string pwd = txtPWD.Text;            MessageBox.Show(user + "--" + pwd);        }        private void Button_Click_1(object sender, RoutedEventArgs e)        {            Application.Current.Shutdown();        }        private void txtUser_GotFocus(object sender, RoutedEventArgs e)        {            TextBox txt = (TextBox)sender;            if (txt.Text == txt.Tag.ToString())            {                txt.Text = "";            }        }        private void txtPWD_LostFocus(object sender, RoutedEventArgs e)        {            TextBox txt = (TextBox)sender;            if (txt.Text == "")            {                txt.Text = txt.Tag.ToString();            }        }    }}

运行后效果:



最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台