110,566
社区成员
发帖
与我相关
我的任务
分享
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;
}
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值
}
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();
}
}
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呢