tags:

views:

416

answers:

5

Hello, I'm learning MVVM through a project and I got stuck on something simple.

I have a button that updates a Listview. I have a command in the ViewModel that make the right things but I want to select the new row and get the focus on a textbox after I click the button.

The question is: How do I update my UI after executing a command?

If I need to change my windows Title when an operation have been made, I use a property on the ViewModel that is binded to the Window's title and it's changed when I need it but, I don't know how to get focus on a control when a command has been executed.

Thank you.

A: 

Hi Jesus

What about setting focus to the control in the code behind: textBox.Focus() I consider everything you mention in your question to be GUI logic so I would add a Click event to the button to handle stuff that needs to happend in the GUI.

Hope this helps.

Fossmo
Yes I tried that, but If you have the command and the event at the same time both are executed. The Event will be executed before than the command (That is what I saw). I need the event to be executed after the command because I need to set the focus when the command do its job. If I delete the command in the button and I execute it inside the event, I lost the CanExecute thing because CanExecute works if you have the command attached to the control.
Jesus Rodriguez
Do you need to call the command from XAML? Can't you just call it from the Click event? Get the command from the datacontext and the execute from there?
Fossmo
As I said, if I call the command from the click event, I lost the CanExecute thing.
Jesus Rodriguez
A: 

Hi Jesus,

I think you need to use the Mediator pattern. Please see this:

Josh Smith's Mediator Prototype for WPF Apps

This is generally used in communicating with the View from the View-Model. Hope this helps.

Michael Detras
+1  A: 

To select the new row, add a new property to your ViewModel ("SelectedItem" for instance), and bind the ListView's SelectedItem property to it :

<ListView ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">...

In the ViewModel, you just have to assign the new item to the SelectedItem property

To focus the TextBox, Mike's idea seems a good one

Thomas Levesque
A: 

You could make an attached behavior. I'd suggest using the new Blend behavior framework, ie TriggerAction that contained this custom logic.

For your attached behavior you put on the button, give it a DP for an ICommand and maybe a DP of a ListView type.

On the "protected override void Invoke(object parameter)" of your TriggerAction, execute your ICommand, then you have reference to your ListView. Here you can do your custom code on it, like setting focus.

Your XAML may look something like this:

<Button>
   <i:Interaction.Triggers>
      <i:EventTrigger EventName="Click">
         <Behaviors:CustomBehavior Command="CommandName" ListView="{Binding ElementName=myListView}" />
      </i:EventTrigger>
   </i:Interaction.Triggers>
<Button/>

I suggest looking at Mike Brown's ExecuteCommandAction behavior (download here), it's about almost 1/2 of what you need.

Jeremiah Morrill
A: 

In your case you need some way that the ViewModel notifies the View that it should set the focus on a specific control.

This could be done with an IView interface. The view implements this interface and the ViewModel can call a method of the View over this interface. This way you have still the View and ViewModel decoupled of each other.

How this can be done is shown here:

WPF Application Framework (WAF)

http://waf.codeplex.com

jbe