WPF Binding的代码实现
发布日期:2021-08-13 18:02:33 浏览次数:18 分类:技术文章

本文共 1481 字,大约阅读时间需要 4 分钟。

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;
namespace XamlTest
{
    /// <summary>
    /// Interaction logic for Window7.xaml
    /// </summary>
    public partial class Window7 : Window
    {
        public Window7()
        {
            InitializeComponent();
         
  //Binding b = new Binding();
            //b.Source = s;
            //b.Path = new PropertyPath("Name");
            //BindingOperations.SetBinding(this.txtName, TextBox.TextProperty, b);
            //第二种写法
           
this.txtName.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = s });
        }
        private Student s=new Student();
        private void btn_Click_1(object sender, RoutedEventArgs e)
        {
            s.Name += "Name";
        }
    }

}

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XamlTest
{
    public class Student:INotifyPropertyChanged
    {
        private string name;
        public string Name
        {
            get { return name; }
            set 
            { 
                name = value;
                OnPropertyChanged("Name");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged!=null)
            {
                PropertyChanged.Invoke(this,new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

转载于:https://www.cnblogs.com/dxmfans/p/9434630.html

转载地址:https://blog.csdn.net/weixin_30696427/article/details/99394507 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Vue项目开发目录结构
下一篇:C语言复习6_doWhile循环

发表评论

最新留言

很好
[***.229.124.182]2024年04月03日 12时54分29秒