• ADO.net数据绑定


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    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.Navigation;
    using System.Windows.Shapes;
    
    namespace Wpf数据绑定
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            private Person p1 = new Person();
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                p1.Age = 20;
                p1.Name = "梁朝伟";
                grid1.DataContext = p1;
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show(p1.Age.ToString()+p1.Name);
            }
    
            private void button2_Click(object sender, RoutedEventArgs e)
            {
                p1.Age++;
                p1.Name = "金城武";
            }
        }
    
        class Person:INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private int age;
            private string name;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    this.name = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                    }
                }
            }
    
            public int Age
            {
                get
                {
                    return age;
                }
                set
                {
                    this.age= value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                    }
                }
            }
        }
    }

    同时在前台在想要绑定的控件里写如:Text=“{Binding Name}”

    一般不直接写textbox_1.Text=p1.Name 是为了避免直接操作控件。

  • 相关阅读:
    Cannot resolve symbol 'SpringBootApp
    Java读取ZIP文件ZipEntry.getsize()总是返回-1?
    java 读取zip里面的xml文件
    导出:xml zip
    jquery form submit提交方式
    [转][C#]无法创建虚拟目录。ASP.NET 2.0 尚未在 Web服务器上注册。
    [转][C#].Net反编译利器
    [转][echarts]地图轮播
    [转][C#]AutoFac 使用方法总结
    [转]用代码访问 Https
  • 原文地址:https://www.cnblogs.com/zuochengsi-9/p/4674426.html
Copyright © 2020-2023  润新知