C
C#15mo ago
Elio

❔ Onpropertychanged

Hello, i'd like to trigger OnpropertyChanged(nameof(CurrentProgramBending)) from this code :
public class ProgramBendingSelectViewModel : ViewModelBase
{
public ListingProgramBendingViewModel ListingProgramBendingViewModel { get; }

public ProgramBendingViewModel? CurrentProgramBending => ListingProgramBendingViewModel.CurrentProgramBending;
}
public class ProgramBendingSelectViewModel : ViewModelBase
{
public ListingProgramBendingViewModel ListingProgramBendingViewModel { get; }

public ProgramBendingViewModel? CurrentProgramBending => ListingProgramBendingViewModel.CurrentProgramBending;
}
How i'm i supposed to do because i've already set OnpropertyChanged(nameof(CurrentProgramBending)) inside ListingProgramBendingViewModel but i can't retrieve the event inside ProgramBendingSelectViewModel Here is the original declaration inside ListinProgramBendingViewmodel
public class ListingProgramBendingViewModel : ViewModelBase
{
private readonly ProgramBendingStore _programBendingStore;

public ProgramBendingViewModel? CurrentProgramBending
{
get
{
return _programBendingStore.CurrentProgramBending;
}
set
{
_programBendingStore.CurrentProgramBending = value;
OnPropertyChanged();
}
}
}
public class ListingProgramBendingViewModel : ViewModelBase
{
private readonly ProgramBendingStore _programBendingStore;

public ProgramBendingViewModel? CurrentProgramBending
{
get
{
return _programBendingStore.CurrentProgramBending;
}
set
{
_programBendingStore.CurrentProgramBending = value;
OnPropertyChanged();
}
}
}
12 Replies
Pobiega
Pobiega15mo ago
You need your "outer" viewmodel ProgramBendingSelectViewModel to fire the OPC event
Klarth
Klarth15mo ago
Avoid wrapping properties if you can, especially when it causes a need for synchronization.
Elio
Elio15mo ago
public ProgramBendingViewModel? CurrentProgramBending => ListingProgramBendingViewModel.CurrentProgramBending; this is what you call wrapping right ?
Klarth
Klarth15mo ago
Delete public ProgramBendingViewModel? CurrentProgramBending => ListingProgramBendingViewModel.CurrentProgramBending; and instead use subproperties binding syntax. Yes, you want to avoid that.
Elio
Elio15mo ago
how do i do that
Klarth
Klarth15mo ago
"{Binding ListingProgramBendingViewModel.CurrentProgramBending.SomeProperty}" Assuming the current DataContext is an instance of ProgramBendingSelectViewModel.
Elio
Elio15mo ago
ohhh i can do that ok i though i needed to rewrite my property in my viewmodel to access it in xaml what is the opc event that pobiega mentionned ?
Klarth
Klarth15mo ago
Your usage of OnPropertyChanged. It needs to be raised from the object instance that contains the property. Which is one reason why forwarding / sync'g across object instances is quite awkward. You'd likely need to subscribe to INPC, check for changes, and then raise INPC again on the wrapping VM. (There are a few other ways via concepts like reactive properties, but that's far outside of the scope and still a pain.)
Elio
Elio15mo ago
ok i will keep your first solution i've tried this and it seems to work in first i thought i needed to wrapp my property in order to use it in my viewmodel
Klarth
Klarth15mo ago
You really only do wrapping in the case where you have a domain model or such that you can't bind to. Because: 1. it doesn't support binding and 2. if it could, that breaks MVVM. So you either wrap, manually sync (in commands or such), or do full mapping...depending on how you work with your domain.
Elio
Elio15mo ago
ok there is so much thing i still need to learn 😅 thanks for the responses :3
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.