zoukankan      html  css  js  c++  java
  • WPF数据绑定,参考代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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 Wpf数据绑定;
    
    namespace Wpf数据绑定
    {
        /// <summary>
        /// Window1.xaml 的交互逻辑
        /// </summary>
        public partial class Window1 : Window
        {
            MainWindow wd;   //构建函数传值
            //public Window1(MainWindow w)
            //{
            //    InitializeComponent();
            //    wd = w;
    
            //     aa.Text = wd.aaa.Text+wd.bbb.Password  ;   用窗口传值
            //}
    
            User1 us;  //对象实例化
            public Window1(User1 w)   //用这个类接收新对象
            {
                InitializeComponent();
    
                us = w;             
                aa.DataContext = w;        //获取或设置参与数据绑定时的数据的上下文。和前台进行绑定
    
                bb.SetBinding(TextBox.TextProperty,     //不需要和前台进行绑定,这一局代码就可是实现并显示
                new Binding("Pass") { Source = us, Mode = BindingMode.TwoWay });
    
      
    
                //aa.Text = w.Username;//这句代码为什么不行呢??????
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                
                us.Username = DateTime.Now.ToString("HH:mm:ss.fff");
            }
        }
    }

    类里的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace Wpf数据绑定
    {
        public class User1 : INotifyPropertyChanged   //一个接口
    
        {
            private string code;
    
            public string Code
            {
                get { return code; }
                set { code = value; }
            }
            private string username;
    
            public string Username
            {
                get { return username; }
                set { 
                    username = value;
                    OnPropertyChanged("Username");  //调用下面的方法
                }
            }
    
           
            private string pass;
    
            public string Pass
            {
                get { return pass; }
                set { pass = value; }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;   //用此种方法实现接口
            public virtual void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        
        }
    }


    绑定的代码

  • 相关阅读:
    [转]在nodejs使用Redis缓存和查询数据及Session持久化(Express)
    [转]最常用的商务职场英语邮件100个句式
    [转]玩转Angular2(4)--制作左侧自动定位菜单
    [转]Angular开发(十八)-路由的基本认识
    Consul之:服务注册与发现
    Consul之:key/value存储
    服务容错保护断路器Hystrix之六:服务熔断和服务降级
    延时队列:Java中的DelayQueue
    分布式延迟消息队列实现分析与设计
    基于redis的延迟消息队列设计
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4606865.html
Copyright © 2011-2022 走看看