开发者

What is the best way to handel click-events in MVVM?

What is the best way to handel click-events in MVVM? Are there a best way?

I have found two solutions:

with a relaycommand:

开发者_如何学Python
RelayCommand buttonAddCategory_Click;
public ICommand ButtonAddCategory_Click
{
    get
    {
        return buttonAddCategory_Click ?? (buttonAddCategory_Click = new RelayCommand(param => this.AddCategory(),
                                                                                      param => true));
    }
}

pro: ?; contra: need workaround with events if i would change ui elements like focus

with attached behaviour:

public static bool GetIsResetMouseLeftButtonDown(TreeView treeView)
{
    return (bool)treeView.GetValue(IsResetMouseLeftButtonDownProperty);
}
public static void SetIsResetMouseLeftButtonDown(TreeView treeViewItem, bool value)
{
    treeViewItem.SetValue(IsResetMouseLeftButtonDownProperty, value);
}
public static readonly DependencyProperty IsResetMouseLeftButtonDownProperty =
    DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDown", typeof(bool), typeof(TreeViewBehavior),
    new UIPropertyMetadata(false, OnIsMouseLeftButtonDownChanged));

pro: you have RoutedEventArgs for changes on the ui; contra: access to other controls?

Right now i use both solutions. The RellayCommand in Buttons (with events for ui updates) and the attached behaviour for a treeview to deselect the treeviewitem if a user clicks.


To me there is no simple answer to this question. That's the way I see it:

  • if you have a defined state-change on the VM, expose a RelayCommand which then can be bound to something the triggers it. In 99,9% percent of the cases this is a button/menu-entry. Something where it can be easily used. The cases that are left -> well some workaround might be needed, like calling a method from the view. So a RelayCommand should imho be used if you are really targeting the VM.

  • Focus-changes on the other hand are view-related functionality. Imho this has nothing todo with the WM. That means for me it should be implemented in the view. So to me I'd even go for a straight-forward eventhandler that does the job.

hth, Martin


I like this idea:

UI logic, such as opening new windows, showing/hiding elements, etc. You keep that on the code-behind.

When this 'click' should do something with the model, invoke the action.

So, a button that closes the window and saves something would be defined like this:

<Button  Name="SaveBtnr" VerticalAlignment="Bottom" 
        Command="{Binding Save}" Click="OnSaveClick"
        CommandParameter="{Binding}">Save</Button>

And the handler would be:

private void OnSaveClick(object sender, RoutedEventArgs e)
    {
        //Do UI Stuff
    }

And then your command:

  public void SaveCommand(object parameter)
    {
        //SaveStuff            
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜