当View绑定的ViewModel中的属性是一个类,而又想绑定该属性下的属性,怎么办呢?
欲绑定ViewModle中名为MyCar中的Name属性(DataContext为VM):
VM:
private Car _myCar; public Car MyCar { get { return _myCar; } set { if (value != _myCar) { this._myCar = value; NotifyPropertyChanged("MyCar"); } } }
Car类:
public class Car { private string _name; public string Name { get { return _name; } set { if (value != _name) { this._name = value; NotifyPropertyChanged("Name"); } } } }
View中写绑定:
<TextBlock Text="{Binding MyCar.Name}"/>