|
Views shouldn't really know about each other in MVVM. They typically communicate via a messenger type service so there is a complete disconnect. If the parent view is going to hold a reference to the child view, you can just use the views INPC implementation (since have it already).
|
|
|
|
|
Well, in summary, what I'm hoping to do is get some kind of notification in the parent ViewModel that a property in the child ViewModel has changed.
What about binding? Any way to do this?
Everything makes sense in someone's mind
|
|
|
|
|
How are you creating the child views? In my apps, I never create the VM directly, I just load the window.xaml and that uses a view locator service to instantiate the VM and wire it up.
Another question is how many properties are going to be changing and how often. If we are talking about 2 or 3 properties that will change 2 or 3 times then my answer would be different then if 50 properties were changing 1000s of times.
With the low prop count / low change count, you can use messenger as I mentioned below.
Also, do you care what property is changing? Or do you just want to know that the view is dirty? etc?
If the parent needs to know whenever 1 of 50 properties change and its new value, etc. I'd probably just have the child view use messenger to send open / close messages so the parent can sub to the INPC events in the child using weak events or something like that.
Again, all depends on how much you need to know about the property that changed and how often they are changing, but I threw out a bunch of options for you .
|
|
|
|
|
You can pass an Action delegate from the parent view to the child view.
Whenever the property within the child changes, fire the Action and let the parent method (pointed to by this action) execute.
|
|
|
|
|
No need to re-invent the wheel. There are already mechanisms to do this. Views should not know about each other in such intimate ways .
|
|
|
|
|
I'm confused as to why you'd expect the parent viewmodel (rather than the view) need to know that child viewmodel properties have changed. You talk about views here, but it seems more likely that you are thinking of the view models. One thing to do is to link a view to a viewmodel using a DataTemplate. This is super easy, and says "whenever I want to use this VM, this is the way it will look".
Showing and hiding the views can then be achieved neatly by using something like Josh Smith's Workspace[^] implementation.
|
|
|
|
|
Collin Jasnoch wrote: I think you meant parent view (rather than the view model).
No, I meant parent viewmodel. Which is why I mentioned "the view" in my post. In other words, I was saying that the interaction is actually between the view models rather than views.
|
|
|
|
|
I just posted a clarification.
Thanks
Everything makes sense in someone's mind
|
|
|
|
|
Ok, so I have posted an image of a prototype of my app here
The blue area is the MainWindow with a data context called MainWindowViewModel. The red is the WelcomeView with a data context called WelcomeViewModel.
I want to replace the WelcomeView model with other "inner" views during runtime. Notice that the toolbar and menu are both on the MainWindowView and their commands are bound to properties in the MainWindowViewModel.
So when the WelcomeView, or some other inner view's data changes, I want the Save button and menu bar on the MainWindow to become enabled.
So, I need to have some way of the 2 viewmodels to "talk" to each other.
Hope this helps
Thanks
Everything makes sense in someone's mind
|
|
|
|
|
Oh, OK... I wasn't exactly sure why you wanted to know if a child view property has changed in my original response... was just giving you what you wanted . Now that you have posted more information, I retract my original response and provide you with a new one.
1) normally, views should not know about each other
2) normally toolbar buttons are routed to the ACTIVE view... in your case, what would happen is that the save button would only be enabled if the child view was active. Probably not what you want? Or maybe it is? I.e. if the tree has focus, should the button still be enabled if the view is dirty?
I'd probably have the IsDirty bool flag in the main window class and have the child view send a message through messenger and have the main window class subscribe to the "some property has changed" message.
You do have a messenger service, right? ... thats a key part of MVVM.
|
|
|
|
|
Well, what's happening now is that the toolbar and menu's Command properties in MainWindowView are bound to a bool property called DataChanged, which is essentially dirty flag.
Sounds like you're saying that I shpuld use messaging in the child viewmodels to set this?
Everything makes sense in someone's mind
|
|
|
|
|
Yeah, so you'd have your main window VM subscribe to a "set dirty flag" message and have the child view send it. That way there is no coupling between the two views and neither knows about each other. Its just a random message that anybody can listen to or send.
As I said in my original post though, if your properties are going to change a lot, messenger might be too much overhead.
|
|
|
|
|
I don't think there will be too many property changes, so it may be ok.
Someone else in this thread suggested using an Action delegate. I could set it as a property on the child view? What do you think of that?
Everything makes sense in someone's mind
|
|
|
|
|
Using an action delegate would work of course, but it introduces tight coupling between view models which is a no-no. vmA has to know explicitly about vmB and explicitly subscribe to the event. Messenger doesn't have that problem since its more "anonymous". All your VM is coupled too is the messenger service. And if you are using DependencyInjection that can be a loose coupling as well.
|
|
|
|
|
There's still one issue to solve on this.
When a RecentProjects link in the child view is clicked, I need to send a message to the MainWindowViewModel to open the project. But how do I pass the ProjectId as a param? I'm using MVVM Light Toolkit from Galasoft, but I don't know it passing params is an option. Ever done this before?
Everything makes sense in someone's mind
|
|
|
|
|
I've looked at MVVM Light, but haven't used it specifically. They do have a Messenger service, that I do know. I think you just derive an object from one of their base classes and you can pass whatever data you want.
|
|
|
|
|
I want to display some text on a user control that is dynamic in what is says, and also that would have hyperlinks in it.
How do I do this?
Everything makes sense in someone's mind
|
|
|
|
|
1) FlowDocument
2) WebBrowser
3) TextBlock using Runs
4) google for WPF LinkLabel control
|
|
|
|
|
Actually I just came up with the same idea,
I used a flowpanel and a series of lables & hyperlinks.
Thanks
Everything makes sense in someone's mind
|
|
|
|
|
Validation in Windows Presentation Foundation[^]
and
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^]
were both good resources for custom validation rules on a DataGrid, but if I programmatically change values in the row source, the row doesn't re-validate. If I change a cell value by manually editing it, on the other hand, validation will fire.
How can I force validation even when the user is not directly manipulating the cell?
I have my custom validation rule ValidationStep set to CommittedValue and UpdatedValue.
I've also tried BeginEdit and CommitEdit after changes are made.
Any thoughts?
|
|
|
|
|
Does the class representing a row of data implement and use the INotifyPropertyChanged interface?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
It's an EF ComplexObject using the default auto generated code. It is derived from StructuralObject, which is derived from INotifyPropertyChanging and implements the INotifyPropertyChanged.
I checked the Properties I'm changing and they all call the ReportPropertyChanged method after a change occurs.
I think this has more to do with the way I'm updating the row source. I'm allowing the user to open a context menu through the ValidationErrorTemplate and select various options. The change occurs in the MenuItem click event. If I change focus to anything in the Window using the mouse, the row will validate.
|
|
|
|
|
|
You're right. I'm not using the validator as intended. There are plenty of things I can do in a cell data template to get my intended functionality. Thanks for the help. It didn't get me to my original goal, but illuminated the error of my ways and saved me time digging for an answer that doesn't exist.
|
|
|
|
|
Hi,
Is there a free ribbon tool bar? Can I use MS office ribbon control in the siverlight apps?
Best regards,
|
|
|
|