Ho un ViewModel con un tipo di proprietà complessa e desidero associare la mia vista a una proprietà nidificata di questo oggetto.Notifica di aggiornamento del binding WPF per la proprietà nidificata quando la proprietà parent cambia
My ViewModel sta implementando INotifyPropertyChanged
(oppure è necessario estenderlo a BaseViewModel
). La classe della proprietà padre non sta implementando INotifyPropertyChanged
.
Quando si aggiorna il valore della proprietà padre, la proprietà nidificata non si aggiorna. Puoi dirmi come posso implementare questa funzionalità?
ViewModel
public class ViewModel : BaseViewModel
{
private Car _myCarProperty;
public Car MyCarProperty
{
get { return _myCarProperty; }
set
{
if (value == _myCarProperty) return;
_myCarProperty = value;
OnPropertyChanged();
}
}
}
rilegatura in vista
<TextBlock Text="{Binding Path=MyCarProperty.Manufacturer}" />
Quando cambio il valore della MyCarProperty
View non si aggiorna.
Grazie per qualsiasi aiuto!
Edit: OnPropertyChanged() implementazione
#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
hai avvisato la proprietà del bambino? –
La classe 'Car' non sta implementando' INotifyPropertyChanged'. Ma non sto cambiando la proprietà 'Manufacturer', cambio la proprietà' MyCarProperty', e quindi mi aspetto che l'evento OnNotifyPropertyChanged attiverà l'aggiornamento del valore?- per favore vedi la mia domanda modificata –
Puoi mostrarci come è implementato OnPropertyChanged? –