Prism/mvvm: how to bind GotFocus of View to an action? - mvvm

I've got a prism/mvvm view and want to notify the ViewModel if the View has got or lost the focus.
I'd guess I'd need to bind GotFocus of the View to an action in the ViewModel, but I got no idea how to get started on this.
Sure this is a standard problem which has been solved somewhere, and it's just me not finding the solution?

You could use InvokeCommandAction behavior. This behavior is defined in the assembly System.Windows.Interactivity which is part of Expression Blend. With this behavior you could bind to the GotFocus event of your view and execute a command in your viewModel.
The same approach can you use for LostFocus. Here´s an example how to use InvokeCommandAction.
One thing about GotFocus of UserControl. You should know that the GotFocus event of the View is raised when a control, such as TextBox, gets the focus. You can´t focus the UserControl by its self.
[Update]
GotFocus of the UserControl is raised when IsTabStop is set to true
Can´t you use the IActiveAware interface of the prism framework. The IsActive property gets set when the view gets navigated in the region.
The interface can be implemented on the View and the ViewModel (requires that the viewmodel instance is set as DataContext of the view) to be notified when the view is activated in the region.

Related

Global behaviour for event in root model class

I want to know what is best practice when triggering an event from a base model class, which will have a common outcome on all View Controllers. Say I have a shared, global variable, which can trigger an event based on different inputs. Wherever I am in my app, I wish to show a popup with the same information. With my knowledge of view controllers and swift, my only option is to implement the same code in each view controller, as I have to add that same popup to each view, depending on where I am.
Wouldn't it be nice if I could pop that popup from a global-root'ish view controller?
Is there a general coding-practice I've missed?
My first idea is to use a navigation controller (UINavigationController) as the root controller, and then just use
navigationController?.topViewController
from any view controller added to that navigation controller.
There might appear a problem: in case if you already displayed the alert view controller, the app will try to present an alert controller on top of another one. You can keep some kind of global state, or check the type of topViewController.

MVVM Binding for Control Text

I've got a Silverlight client and I'm using MVVM. I've put in my first View and View Model. The View binds to the View Model. I now want to localise the text of all the controls. For example my View has a button which contains the text "Search". I know I need to bind the Text content property of the button to something to provide the correct text. The question is what? Do I provide a property in the View Model called SearchButtonText and bind it to that(where the SearchButtonText property returns a resource string)? Or is this taking it too far in terms of what the View Model does and instead bind to a resource string in the View namespace, even though the root level binding for the View is the View Model?
Any help much appreciated!
Cheers
Bind to a resource string in the view.

Clarification of MVVM - interactions between views

I'm using WPF and trying to program the MVVM way.
I understand how every view has its own view model and this works quite well. I am struggling to manage the interaction between views though.
Say I have two views, View1 and View2, each with its own ViewModel, ViewModel1 and ViewModel2. If I have a combobox on View1 and a button, what is the correct way to close the first view, notify the second view of the selection and show the second view once the button is pressed? It doesn't seem like it should go in the model because it's a UI thing. The ViewModel shouldn't know how to open and close WPF forms (or should it?) And the views shouldn't know about any other ViewModels (or should they?)
So how are these problems solved? In a nutshell:
1) How is data passed between views?
2) What manages the lifetime/visibility of views?
It will depend on whether you are doing view model or view first, and the exact implementation details will depend on if you are using an MVVM framework. If you aren't using a framework, then I would strongly recommend you start using one.
In your example, when the button is pressed, a method on ViewModel1 will be invoked. If doing view model first (which I would recommend), you would instantiate an instance of ViewModel2, and at this point you could pass in the combobox selection to the constructor of ViewModel2.
Depending on your framework, there will be different ways of then displaying the view associated with ViewModel2.
For 1) you can sychronize data through the DataModel. Provided each view shares the same instance of the DataModel and it implements INotifyPropertyChanged multiple views can be updated simulateneously.
Your sesond question is a matter of design, as #devdigital states it can depend on whether it is View first or ViewModel first. I would consider the introduction of a Controller class in much as the same way ASP.Net MVC works which controls which view is displayed. You can expose a ViewClosed event on the ViewModel which the controller can listen to and based on your workflow open another view.
You might consider introducing Controllers which are responsible for the lifetime management of the ViewModels. Furthermore, they mediate between the ViewModels.
The sample applications of the WPF Application Framework (WAF) show how these Controllers can be implemented.

ASP.Net MVC UserControl in View with different controller?

I am trying to create a usercontrol that is an extremely simple form. This usercontrol will appear in a number of different views in my app. I am thoroughly confused on how this can be accomplished.
I have created a controller, and then created a usercontrol that uses that controller.
I then created another controller and created a view for an index of that controller. Inside this view i added my usercontrol reference:
<% Html.RenderPartial("~/Views/UserControlController/Create.ascx"); %>
When i attempt to navigate to the view i can see that the actionresult method for the usercontrol in UserControlController is never called.. What am i missing?? thanks for any help.
Ok, I see your problem. Dont Render partial, you want render action. Whats the difference? Render Partial should read RenderPartialView, not RenderPartialAction. If you want to execute the action, you need Html.RenderAction.

MVVM + Datacontext + DataTemplate + Blend = problems

I'm currently using MVVM in a WPF project, all works very well.
I have one Master view and many Detail views that I manage using a currentView property in my MasterViewModel. By using a datatemplate, I bind a view to a viewmodel.
In fact, my master view has a contentcontrol whose content property is binded to my CurrentView property. When I set this currentview property to a viewmodel or another, it calls the corresponding template.
My problem is that using this, my detail views doesn't have explicit datacontext because it is placed by my datatemplate. So in blend, when I open my view to edit its design, I have no datas to bind to my view. If I set a datacontext to my detailview, in blend I can see all datas I can bind but in runtime, the datacontext set by datatemplate is overrided by the datacontext set in my detailview, so I have no datas during runtime.
Does anyone knows how I can create a good MVVM project, with views managed by datatemplates and with datacontext that we can see with Blend ?
Thanks,
I've got a blog post on this issue: http://www.robfe.com/2009/08/design-time-data-in-expression-blend-3/
My post is all about showing data in blend without having to have that data displayed or even created at runtime.
I solved a similar issue in this post:
How can I use Expression Blend to edit a DataTemplate created in Visual Studio?