C
C#2y 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
Pobiega2y ago
You need your "outer" viewmodel ProgramBendingSelectViewModel to fire the OPC event
Klarth
Klarth2y ago
Avoid wrapping properties if you can, especially when it causes a need for synchronization.
Elio
ElioOP2y ago
public ProgramBendingViewModel? CurrentProgramBending => ListingProgramBendingViewModel.CurrentProgramBending; this is what you call wrapping right ?
Klarth
Klarth2y ago
Delete public ProgramBendingViewModel? CurrentProgramBending => ListingProgramBendingViewModel.CurrentProgramBending; and instead use subproperties binding syntax. Yes, you want to avoid that.
Elio
ElioOP2y ago
how do i do that
Klarth
Klarth2y ago
"{Binding ListingProgramBendingViewModel.CurrentProgramBending.SomeProperty}" Assuming the current DataContext is an instance of ProgramBendingSelectViewModel.
Elio
ElioOP2y 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
Klarth2y 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
ElioOP2y 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
Klarth2y 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
ElioOP2y ago
ok there is so much thing i still need to learn 😅 thanks for the responses :3
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server