How to access view in view model
-
Dear friends i have an doubt, i have Page 1,Page 2 and view model for both. now i have button in page 1 that is bound to a command and now when i click that button it goes to view model command (and now here i want to open the second screen but second screen in view) tel me how to access view objects in view model? any idea? link? by Joe.I
-
Dear friends i have an doubt, i have Page 1,Page 2 and view model for both. now i have button in page 1 that is bound to a command and now when i click that button it goes to view model command (and now here i want to open the second screen but second screen in view) tel me how to access view objects in view model? any idea? link? by Joe.I
Joe - it's generally considered to be a bad idea to put knowledge about a view into the viewmodel. This can cause you problems when it comes to testing your VM code because you now have an implicit link to a UI object in place. Saying that, this is a guideline not a hard and fast rule - if you can't get an alternative approach to work and you end up contorting yourself through all sorts of hoops then just do what you need to do. However, one approach that I like is to use typed DataTemplates and let the binding engine take care of creating the relevant objects for me. Josh Smith explains it brilliantly in his MSDN article here[^]. Take a look at the section "Applying a View to a ViewModel" to get an understanding of how it all works.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Dear friends i have an doubt, i have Page 1,Page 2 and view model for both. now i have button in page 1 that is bound to a command and now when i click that button it goes to view model command (and now here i want to open the second screen but second screen in view) tel me how to access view objects in view model? any idea? link? by Joe.I
I don't mean to be rude, but if you are trying to access your view in your VM, then your OOP skills need some refactoring. To try and be not such an ahole...I think what you are saying is that you have a view that contain another view and they share a VM. The main view we can call V1 - the subordinate we call V2. I'm guessing your V2 is simply not visible. In this case, you want to bind the visibility to a property on your VM (like IsOrdersLoaded or something) and use bool2invis converter. When you command fires, it should simply set this property to true (and your properties setter should call OnPropertyChanged("IsOrdersLoaded")). On the other hand - if you are trying to "inject" V2 into V1 (or anywhere else in your layout) and it needs to be dynamic (i.e. the command operation knows what V2 should be) - then you should consider basically the same approach, but your V1 (or whatever else) should contain a ContentPresenter that is capable of taking content as a view. This gets a bit trickier when using Commands since they don't return anything - and the one excpetion to the rule (imo) where View Models "know" about views. Essentially your content presenter's content is bound to a property on your view model via a property something like ... internal FrameworkElement SubContent { get { return this.subContent; } set { this.subContent = value; OnPropertyChanged("SubContent"); } } and this command of your's simply executes to instantiate your new view and sets that view to this.subContent. Visibility of the content presenter should be implemented as a "is content null" kind of value converter. Honestly, I personaly hate this approach - I much prefer command repositories that are decoupled from my MV (so the command itself isn't even in the MV) - makes much more sense for testing when I can spin off views from unit tests and have nothing to do with my V1 or MV. If you went this direction, however, you need to add OnCommandExecuted type events to your own command infrastructure to capture the T and propegate back up to the code-behind in the view, which simply sets this.V2Container.Content = T.View type of thing. I hope that helps a little? If you can be a little more clear about your use case, I can certainly be more helpful. -dec