如何WPF按钮绑定到ViewModelBase命令? [英] How to bind WPF button to a command in ViewModelBase?

查看:529
本文介绍了如何WPF按钮绑定到ViewModelBase命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点 AttributeView 包含各种属性。还有一个按钮,当pressed,应该设置默认值的属性。我也有一个 ViewModelBase 类,是我把所有的ViewModels的基类。
问题是,我似乎无法得到绑定到WPF命令的按钮。

我试过这个,但它只是没有做任何事情:

 <按钮命令={结合DataInitialization}CONTENT ={X:静态localProperties:Resources.BtnReinitializeData}>< /按钮>

该命令定义(在 ViewModelBase )是这样的:

 公共CommandBase DataInitialization {搞定;保护套; }

和在应用程序启动的命令,创建一个新的实例

  DataInitialization =新DataInitializationCommand()

不过,WPF绑定似乎没有查找命令(pressing按钮什么都不做)。在当前视图中使用的视图模型从 ViewModelBase 导出。还有什么我可以试试(我很新的WPF所以这可能是一个很简单的问题)?


解决方案

 <网格和GT;
    < Grid.ColumnDefinitions>
        &所述; ColumnDefinition宽度=*/>
    < /Grid.ColumnDefinitions>
    <按钮命令={结合点击指令}WIDTH =100HEIGHT =100CONTENT =wefwfwef/>
< /网格和GT;

背后的code的窗口:

 公共部分类主窗口:窗口
{
    公共主窗口()
    {
        的InitializeComponent();
        的DataContext =新ViewModelBase();
    }
}

和视图模型:

 公共类ViewModelBase
{
    公共ViewModelBase()
    {
        _canExecute = TRUE;
    }
    私人的ICommand _clickCommand;
    公众的ICommand点击指令
    {
        得到
        {
            返回_clickCommand? (_clickCommand =新CommandHandler(()=> MyAction(),_canExecute));
        }
    }
    私人布尔_canExecute;
    公共无效MyAction()
    {    }
}
公共类CommandHandler:ICommand的
{
    私人诉讼_action;
    私人布尔_canExecute;
    公共CommandHandler(动作的动作,布尔canExecute)
    {
        _action =行动;
        _canExecute = canExecute;
    }    公共BOOL CanExecute(对象参数)
    {
        返回_canExecute;
    }    公共事件的EventHandler CanExecuteChanged;    公共无效执行(对象参数)
    {
        _行动();
    }
}

我希望这会给你的想法。

I have a view AttributeView that contains all sorts of attributes. There's also a button that when pressed, it should set the default values to the attributes. I also have a ViewModelBase class that is a base class for all ViewModels I have. The problem is I can't seem to get the button bound to the command with WPF.

I've tried this, but it just doesn't do anything:

<Button Command="{Binding DataInitialization}" Content="{x:Static localProperties:Resources.BtnReinitializeData}"></Button>

The command is defined (in the ViewModelBase) like this:

public CommandBase DataInitialization { get; protected set; }

and on application startup a new instance is created for the command:

DataInitialization = new DataInitializationCommand()

However, the WPF binding doesn't seem to "find" the command (pressing the button does nothing). The ViewModel used in the current view is derived from the ViewModelBase. What else I can try (I'm quite new to WPF so this might be a very simple question)?

解决方案

 <Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Button Command="{Binding ClickCommand}" Width="100" Height="100" Content="wefwfwef"/>
</Grid>

the code behind for the window:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModelBase();
    }
}

And the ViewModel:

public class ViewModelBase
{
    public ViewModelBase()
    {
        _canExecute = true;
    }
    private ICommand _clickCommand;
    public ICommand ClickCommand
    {
        get
        {
            return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), _canExecute));
        }
    }
    private bool _canExecute;
    public void MyAction()
    {

    }
}
public class CommandHandler : ICommand
{
    private Action _action;
    private bool _canExecute;
    public CommandHandler(Action action, bool canExecute)
    {
        _action = action;
        _canExecute = canExecute;
    }

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

    public event EventHandler CanExecuteChanged;

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

I hope this will give you the idea.

这篇关于如何WPF按钮绑定到ViewModelBase命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆