Tôi có một ViewModel với kiểu thuộc tính phức tạp và muốn ràng buộc chế độ xem của tôi với thuộc tính lồng nhau của đối tượng này.Thông báo cập nhật liên kết WPF cho thuộc tính lồng nhau khi thuộc tính cha mẹ thay đổi
My ViewModel đang triển khai INotifyPropertyChanged
(hoặc được mở rộng BaseViewModel
đang triển khai). Lớp thuộc tính cha mẹ không triển khai INotifyPropertyChanged
.
Khi tôi cập nhật giá trị của thuộc tính gốc, thuộc tính lồng nhau không cập nhật. Bạn có thể cho tôi biết làm thế nào tôi có thể thực hiện chức năng này?
ViewModel
public class ViewModel : BaseViewModel
{
private Car _myCarProperty;
public Car MyCarProperty
{
get { return _myCarProperty; }
set
{
if (value == _myCarProperty) return;
_myCarProperty = value;
OnPropertyChanged();
}
}
}
Binding trong Xem
<TextBlock Text="{Binding Path=MyCarProperty.Manufacturer}" />
Khi tôi thay đổi giá trị của MyCarProperty
View không cập nhật.
Cảm ơn bạn đã trợ giúp!
Edit: OnPropertyChanged() thực hiện
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion INotifyPropertyChanged
bạn có thông báo cho tài sản trẻ em không? –
Lớp 'Xe' không triển khai' INotifyPropertyChanged'. Nhưng tôi không thay đổi thuộc tính 'Manufacturer', tôi thay đổi thuộc tính' MyCarProperty', và vì vậy tôi hy vọng rằng sự kiện OnNotifyPropertyChanged sẽ kích hoạt cập nhật giá trị?- vui lòng xem câu hỏi đã chỉnh sửa của tôi. –
Bạn có thể cho chúng tôi biết cách OnPropertyChanged được triển khai không? –