从viewmodel调用方法的命令

时间:2012-06-16 02:00:52

标签: c# wpf xaml mvvm command

好吧,我倾向于避免使用命令,因为他们总是设法让我感到困惑,但是我正在进行一个新项目,我正在尝试正确地构建它,而我的视图背后没有代码。基本上我现在正试图做的就是连接一个按钮,它触发一个命令,在我的视图模型上做一些事情,不知怎么这么简单仍然给我带来麻烦。我想我很接近但不能完全到达那里。这就是我现在所拥有的。

<Window.Resources>
    <RoutedUICommand x:Key="GetMusic" />
</Window.Resources>
<Window.DataContext>
    <core:ViewMain />
</Window.DataContext>
<Window.CommandBindings>
    <CommandBinding Command="{StaticResource GetMusic}" Executed="GetMusicExecuted"/>
</Window.CommandBindings>

视图模型现在几乎没什么了

public class ViewMain
{
    public MusicCollection Music { get; set; }

    private void GetMusicExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        //Logic
    }
}

现在我要做的是连接我在命令绑定中设置的命令,只是在我的视图模型中调用我执行的方法,但它试图在视图本身中找到该方法。有没有办法可以将它引导到我的视图模型中的方法,或者更好的方法来设置它来完成同样的事情?希望一开始就保持简单,所以我不会太早打扰我的想法。

1 个答案:

答案 0 :(得分:11)

我倾向于使用自己的命令类,实现ICommand。然后我将Button Command属性绑定到视图模型中的command属性。单击该按钮时,它将执行任何绑定到Command属性的Execute方法。

这是丑陋的两分钟版本,但它显示了如何创建一个Command类,然后为它指定委托,指向您在视图模型上喜欢的任何方法。

ViewModel:

public class MyViewModel
{
    public MyCommand ActionCommand
    {
        get;
        set;
    }

    public MyViewModel()
    {
        ActionCommand = new MyCommand();
        ActionCommand.CanExecuteFunc = obj => true;
        ActionCommand.ExecuteFunc = MyActionFunc;
    }

    public void MyActionFunc(object parameter)
    {
        // Do stuff here 
    }

}

public class MyCommand : ICommand 
{
    public Predicate<object> CanExecuteFunc
    {
        get;
        set;
    }

    public Action<object> ExecuteFunc
    {
        get;
        set;
    }

    public bool CanExecute(object parameter)
    {
        return CanExecuteFunc(parameter);
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        ExecuteFunc(parameter);
    }
}

View会这样绑定它(假设DataContext设置为视图模型的一个实例):

<Window x:Class="exp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Command="{Binding Path=ActionCommand}">Action</Button>
    </Grid>
</Window>