首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在ViewModelBase中绑定WPF按钮到命令?

如何在ViewModelBase中绑定WPF按钮到命令?
EN

Stack Overflow用户
提问于 2012-09-14 18:45:36
回答 1查看 226.8K关注 0票数 63

我有一个包含各种属性的视图AttributeView。还有一个按钮,当按下时,它应该设置属性的默认值。我还有一个ViewModelBase类,它是我拥有的所有ViewModels的基类。问题是我似乎不能用WPF将按钮绑定到命令上。

我试过了,但它什么也做不了:

代码语言:javascript
复制
<Button Command="{Binding DataInitialization}" Content="{x:Static localProperties:Resources.BtnReinitializeData}"></Button>

命令的定义(在ViewModelBase中)如下所示:

代码语言:javascript
复制
public CommandBase DataInitialization { get; protected set; }

在应用程序启动时,将为该命令创建一个新实例:

代码语言:javascript
复制
DataInitialization = new DataInitializationCommand()

然而,WPF绑定似乎没有“找到”命令(按下按钮什么也不做)。当前视图中使用的ViewModel派生自ViewModelBase。我还可以尝试什么(我是WPF的新手,所以这可能是一个非常简单的问题)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-14 19:51:11

代码语言:javascript
复制
 <Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Button Command="{Binding ClickCommand}" Width="100" Height="100" Content="wefwfwef"/>
</Grid>

窗口的代码幕后:

代码语言:javascript
复制
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModelBase();
    }
}

ViewModel:

代码语言:javascript
复制
public class ViewModelBase
{
    private ICommand _clickCommand;
    public ICommand ClickCommand
    {
        get
        {
            return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), ()=> CanExecute));
        }
    }
     public bool CanExecute
     {
        get
        {
            // check if executing is allowed, i.e., validate, check if a process is running, etc. 
            return true/false;
        }
     }

    public void MyAction()
    {

    }
}

命令处理程序:

代码语言:javascript
复制
 public class CommandHandler : ICommand
{
    private Action _action;
    private Func<bool> _canExecute;

    /// <summary>
    /// Creates instance of the command handler
    /// </summary>
    /// <param name="action">Action to be executed by the command</param>
    /// <param name="canExecute">A bolean property to containing current permissions to execute the command</param>
    public CommandHandler(Action action, Func<bool> canExecute)
    {
        _action = action;
        _canExecute = canExecute;
    }

    /// <summary>
    /// Wires CanExecuteChanged event 
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    /// <summary>
    /// Forcess checking if execute is allowed
    /// </summary>
    /// <param name="parameter"></param>
    /// <returns></returns>
    public bool CanExecute(object parameter)
    {
        return _canExecute.Invoke();
    }

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

我希望这能给你一些灵感。

票数 143
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12422945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档