关于ObservableObject,wpf的对象绑定问题

我叫小菜菜 2016-10-19 11:45:39
ObservableCollection <(Of <(T>)>) 类..............为了完全支持将绑定源对象中的数据值传送到绑定目标,在支持可绑定属性的集合中的每个对象都必须实现适当的属性更改通知机制,如 INotifyPropertyChanged 接口。

这个ObservableCollection是集合类型,可以使用WPF绑定的。
对单个自定义对象,有类似的ObservableObject<T>不?

ObservableCollection<T>是已定义的类型,工作得很好,但是对于单个对象的,貌似没有找到哦,请问有相关的类似概念吗,比如ObservableObject<T>,BindingObject之类的。

应用场景:后台一个自定义class对象跟前台的输入框一一绑定,不用想winform的每次都setText和getText
...全文
901 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Forty2 2016-10-20
  • 打赏
  • 举报
回复
引用 6 楼 zhi_ai_yaya 的回复:
this.tb_idno.SetBinding(TextBox.TextProperty, new Binding("Addr") { Source = my.house });
引用 7 楼 zhi_ai_yaya 的回复:
my.house = new House() { Addr="new Addr",Id="new Id"};//无法同步到textbox值
你觉得这种绑定可行吗?或者你认为如下代码能改变string a吗? string a = my.Name; my.Name = "Bob Dylan"; // 这个能改变a吗? 因此 1、你的绑定要类似: this.tb_idno.SetBinding(TextBox.TextProperty, new Binding("house.Addr") { Source = my }); 2、Person类的public House house { get; set; }同样要写成类似Name属性的方式(支持变化通知)。
Forty2 2016-10-19
  • 打赏
  • 举报
回复
引用 2 楼 zhi_ai_yaya 的回复:
... NotifyPropertyChanged("Source");////代码有点多而且重复 ...
为了程序正确,目前那些重复还是必要的。 是有一些方法可以“去掉”那些代码,比如说: 1、后编译织入(需要工具) 2、用代理对象等 但就总的维护成本来说(易读,易改,易生成等指标),“重复那些代码”可能是更好的选择。
我叫小菜菜 2016-10-19
  • 打赏
  • 举报
回复
引用 1 楼 Forty2 的回复:
[quote=引用 楼主 zhi_ai_yaya 的回复:] ...每个对象都必须实现适当的属性更改通知机制,如 INotifyPropertyChanged 接口...
单个对象,那就是INotifyPropertyChanged :) [/quote] ---- 摘: 通常情况下,一个实现了INotifyPropertyChanged接口的可绑定类型的基类也常常需要是可绑定的。在这种情况下,软件开发人员可以考虑将INotifyPropertyChanged接口实现为一个基类BindableBase,并令那些可绑定类型派生于该类。但是在选择该做法之前,软件开发人员需要仔细考虑编程语言的单继承特性。这不仅仅决定于当前的类型继承关系,更会影响到程序的扩展性。   如果软件开发人员希望一个集合参与绑定,并且在集合发生变化,如插入、移动数据等操作时使绑定工作,那么他需要使用实现INotifyCollectionChanged接口的集合类型。WPF为软件开发人员提供了一个较为有用的实现该接口的集合类型:ObservableCollection。[Object类型的有木有有木有木有?]同时该类还实现了INotifyPropertyChanged接口,因此该集合类型暴露的各个属性也是可绑定的。 ----------------- 必须自己实现INotifyPropertyChanged接口接口,处理各个属性细节?像下面的,每个属性都在set访问器里面触发NotifyPropertyChanged,这代码有点多而且重复哦。
public class DataSource : INotifyPropertyChanged
{
    protected void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public string Source
    {
        get { return mSource; }
        set
        {
            if (mSource == value)
                return;

            mSource = value;
            NotifyPropertyChanged("Source");////代码有点多而且重复
        }
    }

    private string mSource = string.Empty;
}
Forty2 2016-10-19
  • 打赏
  • 举报
回复
引用 楼主 zhi_ai_yaya 的回复:
...每个对象都必须实现适当的属性更改通知机制,如 INotifyPropertyChanged 接口...
单个对象,那就是INotifyPropertyChanged :)
我叫小菜菜 2016-10-19
  • 打赏
  • 举报
回复
6楼存在的问题是,直接赋值class类型的属性无法同步绑定。
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            i++;
            my.Name = "后台修改了Name" + i.ToString();
            my.house = new House() { Addr="new Addr",Id="new Id"};//无法同步到textbox值
        }
我叫小菜菜 2016-10-19
  • 打赏
  • 举报
回复
嵌套绑定问题:demo,麻烦大家分析下可行否
namespace WpfApp测试自定义class类型绑定
{

    public class House : ObservableObject
    {
        private string addr;
        private string id;

        public string Addr
        {
            get { return this.addr; }
            set
            {
                this.SetAndNotify(ref this.addr, value, () => this.Addr);
                Console.WriteLine("set value:" + value);
            }
        }

        public string Id
        {
            get { return this.id; }
            set
            {
                this.SetAndNotify(ref this.id, value, () => this.Id);
                Console.WriteLine("set value:" + value);
            }
        }
    }


    public class Person : ObservableObject
    {
        private string name;
        public House house { get; set; }

        public string Name
        {
            get { return this.name; }
            set
            {
                this.SetAndNotify(ref this.name, value, () => this.Name);
                Console.WriteLine("set value:" + value);
            }
        }
    }
}
    public partial class Window2 : Window
    {
        public Person my { get; set; }
        int i = 0;
        public Window2()
        {
            my = new Person();
            InitializeComponent();
            my.Name = "0";
            my.house = new House();
            my.house.Addr = "地址1";
            my.house.Id = "id1";

            this.tb_name.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = my });
            this.tb_idno.SetBinding(TextBox.TextProperty, new Binding("Addr") { Source = my.house });
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            i++;
            my.Name = "后台修改了Name" + i.ToString();
            my.house.Addr = "后台修改了Addr" + i.ToString();
        }
    }
我叫小菜菜 2016-10-19
  • 打赏
  • 举报
回复
引用 4 楼 duanzi_peng 的回复:
后台一个自定义class对象跟前台的输入框一一绑定 -》那就设置这个输入框的DataContext为class,class并实现INotifyPropertyChanged接口
自己编写class属性并在set访问器中触发NotifyPropertyChanged通知,这个解决了,有好几种方法可参考。 见http://www.cnblogs.com/TianFang/p/3381484.html
        public Window1()
        {
            my = new Customer();
            InitializeComponent();
            my.Name = "0";
            my.PhoneNumber = "0";
            this.tb_name.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = my });
            this.tb_idno.SetBinding(TextBox.TextProperty, new Binding("PhoneNumber") { Source = my });
        }
---------------- 另一个嵌套class的问题: 这是对class的直接属性操作,假如是属性是另一个class对象呢,改如何绑定? 比如:下面这个class,如何绑定

    public class Person
    {
        public string name { get; set; }
        public House house { get; set; }
    }

    public class House
    {
        public string addr { get; set; }
        public string id { get; set; }
    }
如何双向绑定到house.id呢
exception92 2016-10-19
  • 打赏
  • 举报
回复
后台一个自定义class对象跟前台的输入框一一绑定 -》那就设置这个输入框的DataContext为class,class并实现INotifyPropertyChanged接口

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧