wpf登陆及构造函数的传值及绑定传值

属性:

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");
        }
    }
}

  

时间: 2024-08-28 23:11:07

wpf登陆及构造函数的传值及绑定传值的相关文章

WPF 让普通 CLR 属性支持 XAML 绑定(非依赖属性),这样 MarkupExtension 中定义的属性也能使用绑定了

原文:WPF 让普通 CLR 属性支持 XAML 绑定(非依赖属性),这样 MarkupExtension 中定义的属性也能使用绑定了 如果你写了一个 MarkupExtension 在 XAML 当中使用,你会发现你在 MarkupExtension 中定时的属性是无法使用 XAML 绑定的,因为 MarkupExtension 不是一个 DependencyObject. 本文将给出解决方案,让你能够在任意的类型中写出支持 XAML 绑定的属性:而不一定要依赖对象(DependencyObj

Angular中父子组件双向绑定传值

下面为大家展示一个较为简单的ng父子组件双向绑定传值,下面是父组件页面 这个页面的大概功能就是父组件(红色)通过输入框输入内容反映到子组件上进行展示,并且进行了投影, 子组件(橙黄色)通过Input输入框输入内容反映到父组件上,并且使用了@Output传值给父组件 下面是父组件的页面 父组件ts 子组件页面 子组件ts 我主要来说下需要注意的几个地方,第一个就是当我利用子组件的@Output发射数据给父组件的时候, import { EventEmitter } from '@angular/c

【WPF学习】第三十章 元素绑定——绑定到非元素对象

原文:[WPF学习]第三十章 元素绑定--绑定到非元素对象 前面章节一直都在讨论如何添加链接两个各元素的绑定.但在数据驱动的应用程序中,更常见的情况是创建从不可见对象中提取数据的绑定表达式.唯一的要求是希望显示的信息必须存储在公有属性中.WPF数据绑定数据结构不能获取私有信息或公有字段. 当绑定到非元素对象时,需要放弃Binding.ElementName属性,并使用以下属性中的一个: Source:该属性是指向源对象的引用--换句话说,是提供数据的对象. RelativeSource:这是引用

Android 组件系列-----Activity的传值和回传值

在这篇随笔里将讲解Activity的传值和回传值. 一.通过startActivity来进行Activity的传值 在Android中,如果我们要通过一个Activity来启动另一个Activity,可以使用 startActivity(Intent intent)方法来传入一个Intent对象,这个Intent对象我们可以精确的指定我们需要跳转的Activity上,或者通过Intent对象来指定我们要完成的一个action操作. ①.通过setClass方法来指定我们要跳转的Activity

子窗体和父窗体双向传值——C#窗体传值方法总结

简介 在很多场景下,我们的程序需要完成窗体间的传值功能,有时候是父窗体→子窗体单向传值.子窗体→父窗体传值甚至是,也有时候我们需要父窗体?子窗体双向传值. 在本文中主要介绍一些能够实现双向传值的方法,能够双向传值的方法也能够实现单向传值. 本文的所有源码都可以在GitHub上下载. 本文介绍的方法仅限于我自己知道并且实现过的,我相信还有很多我并不知道的方法,因此也许在很多朋友眼中本文的内容是浅显甚至可笑的,希望路过的各方朋友不吝赐教,我也希望不断地进步! 方法1:Public字段+ShowDia

iOS中多视图的传值 属性传值和代理传值

首先创建两个类 ,FirstViewController和SecondViewController,都继承于UIViewController 1 #import "AppDelegate.h" 2 #import "FirstViewController.h" 3 4 @interface AppDelegate () 5 6 @end 7 8 @implementation AppDelegate 9 10 11 - (BOOL)application:(UIAp

iOS 页面间传值 之 属性传值,代理传值

手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但其具有局限性(一般用于将第一个页面的值传递到第二个页面,但无法从第二个页面传到第一个页面), 向SecondViewController传值:SecondViewController 设置属性 sendMessage 1 - (void)rightButtonAction:(UIBarButtonI

属性传值,协议传值,block传值,单例传值四种界面传值方式

一.属性传值 对于属性传值而言,相对于其它的三种 方法来说,是最基础,最简单的一种 方法,但,属性传值 有很大的局限性,因为是适用于第一个界面向第二个界面传 值,第二个向第三个界面传值等等.N界面向N + 1界面传值.而在此基础上,必须知道跳转界面的明确位置及所要传的值的具体类型.在第二个界面中声明所要传值 类型的属性. @interface SecondViewController : UIViewController //声明一个字符串属性来保存第一个界面传过来的字符串内容 @propert

IOS笔记047-代理传值和block传值

在两个不同的控制器之间传递数据,可以使用代理传值或者block传值. 例子是一个简单通讯录. 主界面如下: 添加联系人界面 查看/编辑联系人界面:默认是查看模式,点击编辑后进入编辑模式 编辑模式 数据更新成功. 其中添加联系人界面的数据传递使用代理方式实现. 编辑联系人界面的数据传递使用block实现. 下面来看具体过程 1.整个界面搭建 在storyboard里拖拽四个控制器,其中联系人界面是一个UITableView.界面之间的跳转使用代码实现,但是要给每一个控制器指定一个标识.按功能分别指