如何将WPF按钮绑定到ViewModelBase中的命令?

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


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


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

该命令是在(如下ViewModelBase)中定义的:


public CommandBase DataInitialization { get; protected set; }

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


DataInitialization = new DataInitializationCommand()

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


小唯快跑啊
浏览 922回答 3
3回答

幕布斯7119047

<Grid >&nbsp; &nbsp; <Grid.ColumnDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; <ColumnDefinition Width="*"/>&nbsp; &nbsp; </Grid.ColumnDefinitions>&nbsp; &nbsp; <Button Command="{Binding ClickCommand}" Width="100" Height="100" Content="wefwfwef"/></Grid>窗口背后的代码:public partial class MainWindow : Window{&nbsp; &nbsp; public MainWindow()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; DataContext = new ViewModelBase();&nbsp; &nbsp; }}ViewModel:public class ViewModelBase{&nbsp; &nbsp; private ICommand _clickCommand;&nbsp; &nbsp; public ICommand ClickCommand&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), ()=> CanExecute));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;public bool CanExecute&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // check if executing is allowed, i.e., validate, check if a process is running, etc.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true/false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; public void MyAction()&nbsp; &nbsp; {&nbsp; &nbsp; }}命令处理程序:&nbsp;public class CommandHandler : ICommand{&nbsp; &nbsp; private Action _action;&nbsp; &nbsp; private Func<bool> _canExecute;&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Creates instance of the command handler&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="action">Action to be executed by the command</param>&nbsp; &nbsp; /// <param name="canExecute">A bolean property to containing current permissions to execute the command</param>&nbsp; &nbsp; public CommandHandler(Action action, Func<bool> canExecute)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _action = action;&nbsp; &nbsp; &nbsp; &nbsp; _canExecute = canExecute;&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Wires CanExecuteChanged event&nbsp;&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; public event EventHandler CanExecuteChanged&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; add { CommandManager.RequerySuggested += value; }&nbsp; &nbsp; &nbsp; &nbsp; remove { CommandManager.RequerySuggested -= value; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Forcess checking if execute is allowed&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="parameter"></param>&nbsp; &nbsp; /// <returns></returns>&nbsp; &nbsp; public bool CanExecute(object parameter)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return _canExecute.Invoke();&nbsp; &nbsp; }&nbsp; &nbsp; public void Execute(object parameter)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _action();&nbsp; &nbsp; }}我希望这会给你这个想法。

梵蒂冈之花

ethicallogics在这里提出的解决方案就像一个魅力。只需注意一句 - 不要实现自己的CommandHandler,而应考虑使用Microsoft.Practices.Prism.Commands.DelegateCommand(实际上是相同的,除非您的按钮始终处于启用状态,否则需要额外的ctor)。

收到一只叮咚

要使用_canExecute,您必须实现有关此操作的通知。并且WPF内置了默认机制来重新查询CanExecute属性。但要做到这一点,你的ICommand必须重新订阅默认命令管理器,如下所示:`public event EventHandler CanExecuteChanged {add {CommandManager.RequerySuggested + = value;&nbsp;} remove {CommandManager.RequerySuggested - = value;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP