C# 如何将 WPF 按钮绑定到 ViewModelBase 中的命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12422945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind WPF button to a command in ViewModelBase?
提问by kor_
I have a view AttributeViewthat 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 ViewModelBaseclass 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.
我有一个AttributeView包含各种属性的视图。还有一个按钮,当按下时,它应该为属性设置默认值。我还有一个ViewModelBase类,它是我拥有的所有 ViewModel 的基类。问题是我似乎无法使用 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:
命令是这样定义的(在 中ViewModelBase):
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)?
但是,WPF 绑定似乎没有“找到”命令(按下按钮什么也不做)。当前视图中使用的 ViewModel 派生自ViewModelBase. 我还能尝试什么(我对 WPF 很陌生,所以这可能是一个非常简单的问题)?
采纳答案by yo chauhan
<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();
}
}
The ViewModel:
视图模型:
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()
{
}
}
Command Handler:
命令处理程序:
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();
}
}
I hope this will give you the idea.
我希望这会给你的想法。

