Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
MVVM conundrum
Message
From
06/07/2011 13:37:47
 
 
To
06/07/2011 12:25:53
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Title:
Environment versions
Environment:
C# 4.0
Miscellaneous
Thread ID:
01517403
Message ID:
01517459
Views:
48
>>Hi,
>>
>>Stuck as to the best way to handle this (actually at the moment I'm stuck for *any* way but....)
>>
>>Example of problem:
>>
>>A WPF Window wired up to ViewModelA.
>>Within the Window is a Grid wired up to ViewModelB.
>>The Grid contains a button which, when clicked needs to send some info from ViewModelB to ViewModelA.
>>The command handler in ViewModelB uses the Prism EventAggregator to publish and ViewModelA subscribes.
>>So far, so good BUT:
>>The button should only be enabled only when certain conditions are met in both ViewModelB *and* ViewModelA.
>>ViewModelB is easy since I can use the CanExecute().
>>But how to have the button state also dependent on ViewModelA's conditions?
>>
>>Criteria for solution :
>>ViewModelA and ViewModelB can only communicate via publish/subscribe
>>No code-behind in the Views....
>>
>>I guess I could change things so that ViewModelB is nested within ViewModelA and the button is wired to ViewModelA - but I'd rather avoid that since ViewModelB will not always be relevant to ViewModelA.
>>
>>TIA for any suggestions,
>>Viv
>>
>>UPDATE: Never mind (unless you can think of something more generic) . I just raised an event in ViewModelA when the relevant status changes and subscribe to and track it in ViewModelB.
>
>That works. And if these need to be completely decoupled, that sounds like the best route.
>
>Since ViewModelB sounds tightly coupled to ViewModelA, I wouldn't have a problem with ViewModelB having a reference to ViewModelA. But that would depend on whether whoever creates ViewModelB also knows about ViewModelA. (You may want to use weak references or events.)
>
>When things are tightly coupled, I will often create my ViewModels in a Hierarchy. That way if I have 2 ViewModelA's both with ViewModelB's they can communicate without getting confused.
>
>In this case A does not seem

Looks like you were cut off in mid-flow :-}

This is about the only time that ViewModelA and ViewModelB need to interact and, AFAICS, this is the only other communication they need so I'll leave as is for now. But, thinking about it, it would be a fairly trivial step to combine both VMs under a 'master' VM - the XAML guys would only need to know how to bind to both bits......
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform