标签:
属性:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; namespace ceshi { public class user : INotifyPropertyChanged { private string username; public string Username { get { return username; } set { username = value; OnPropertyChanged("Username"); } } private int usercode; public int Usercode { get { return usercode; } set { usercode = value; } } private string password; public string Password { get { return password; } set { password = value; OnPropertyChanged("Password"); } } public event PropertyChangedEventHandler PropertyChanged;//事件委托 //所谓“事件委托”,一个事件的本质是一个委托(因为事件是委托类型的)。而委托的好处在于它可以动态调用不同类之间具备相同函数签名(函数参数顺序、类型、个数相同),且函数返回值必须完全相同的函数。 public virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
xaml代码:
<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="主页" Height="349.189" Width="387.111" HorizontalContentAlignment="Stretch" Background="#FFB58585" WindowStyle="ToolWindow"> <Grid Margin="39,31,50.8,31.6"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="120"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Content="用户名" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="31,34,0,0"/> <Label Content="密码" HorizontalAlignment="Right" Grid.Row="1" VerticalAlignment="Top" Margin="31,24.8,0,0" RenderTransformOrigin="0.175,0.555"/> <TextBox x:Name="user" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Margin="3,37,0,0" RenderTransformOrigin="0.728,0.39" Grid.Column="1"/> <Button Content="登陆" HorizontalAlignment="Left" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top" Width="75" Margin="10,37.6,0,0" Click="Button_Click_2" Grid.Column="1"/> <PasswordBox x:Name="pwd" Grid.Column="1" HorizontalAlignment="Left" Margin="5,25.8,0,0" Grid.Row="1" VerticalAlignment="Top" Width="118" Height="25"/> </Grid> </Window>
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; using ceshi; namespace WpfApplication3 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } user r = new user();//实例化新对像 public Window1 w; private void Button_Click_2(object sender, RoutedEventArgs e) { //r = new user(); r.Username = "admin";//记录用户名和密码 r.Password = "admin"; if (string.IsNullOrWhiteSpace(user.Text)) { MessageBox.Show("请输入用户名和密码"); } else if (user.Text.ToUpper()==r.Username.ToUpper()&&pwd.Password==r.Password) { if (w==null)//如果为空 新实例化一个 { w = new Window1(r); w.Show(); this.Owner = w; this.Hide(); } } else { MessageBox.Show("登录失败"); } } } }
xaml代码:
<Window x:Class="WpfApplication3.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <TextBox x:Name="a" HorizontalAlignment="Left" Height="23" Margin="83,52,0,0" TextWrapping="Wrap" Text="{Binding Username}" VerticalAlignment="Top" Width="120"/> <TextBox x:Name="b" HorizontalAlignment="Left" Height="23" Margin="83,132,0,0" TextWrapping="Wrap" Text="{Binding Password}" VerticalAlignment="Top" Width="120"/> <Button Content="Button" HorizontalAlignment="Left" Margin="193,194,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/> </Grid> </Window>
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.Shapes; using ceshi; namespace WpfApplication3 { /// <summary> /// Window1.xaml 的交互逻辑 /// </summary> public partial class Window1 : Window { public MainWindow wd; //public Window1(MainWindow w)//构造函数传值传的是整个页面 //{ // InitializeComponent(); // wd = w; // a.Text = wd.user.Text; // b.Text = wd.user.Text; //} user uu; public Window1(user u) { uu = u; InitializeComponent(); a.DataContext = u;//绑定传值 //b.DataContext = u; b.SetBinding(TextBox.TextProperty, new Binding("Username") { Source = uu, Mode = BindingMode.TwoWay }); //a.Text = u.Username;//构造函数传值 //b.Text = u.Password; } private void Button_Click_1(object sender, RoutedEventArgs e) { uu.Username = DateTime.Now.ToString("HH:mm:ss.fff"); } } }
标签:
原文地址:http://www.cnblogs.com/Mr-xue/p/4607261.html