为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
小唯快跑啊 2019-09-02 09:41:00
我有一个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很新,所以这可能是一个非常简单的问题)?
查看完整描述

3 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

<Grid >

    <Grid.ColumnDefinitions>

        <ColumnDefinition Width="*"/>

    </Grid.ColumnDefinitions>

    <Button Command="{Binding ClickCommand}" Width="100" Height="100" Content="wefwfwef"/>

</Grid>

窗口背后的代码:


public partial class MainWindow : Window

{

    public MainWindow()

    {

        InitializeComponent();

        DataContext = new ViewModelBase();

    }

}

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()

    {


    }

}

命令处理程序:


 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();

    }

}

我希望这会给你这个想法。


查看完整回答
反对 回复 2019-09-02
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

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


查看完整回答
反对 回复 2019-09-02
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

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

查看完整回答
反对 回复 2019-09-02
  • 3 回答
  • 0 关注
  • 916 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信