禁用MVVM中的按钮 [英] Disable a button in MVVM

查看:204
本文介绍了禁用MVVM中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮点击事件。



I have a button click event.

private async void Start_Click(object sender, RoutedEventArgs e)
{
     try
     {
         Start.IsEnabled = false;
         Start.IsHitTestVisible = false;
         Start.Background = Brushes.Red;





xaml:





The xaml:

<Button Grid.Column="1" Grid.Row="3" Command="{Binding StartProcess}" Width="200">Start</Button>





现在我将使用MVVM模式然后摆脱代码,代码是什么?假设在你的XAML中使用icommand。



Now I am going to use MVVM pattern then get rid of code behind, what is the code? Suppose use icommand.

推荐答案



in your XAML
<Button Command="{Binding SomeCommand}">Do Something</Button>













public class MyViewModel : ViewModel
{
    private readonly ICommand someCommand;

    public MyViewModel()
    {
        this.someCommand = new DelegateCommand(this.DoSomething, this.CanDoSomething);
    }

    public ICommand SomeCommand
    {
        get { return this.someCommand; }
    }

    private void DoSomething(object state)
    {
        // do something here
    }

    private bool CanDoSomething(object state)
    {
        // return true/false here is enabled/disable button
    }
}


这篇关于禁用MVVM中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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