How does one “disable” a button in WPF using the M

2019-01-12 02:23发布

I'm trying to get a grasp on WPF and MVVM and have been making good progress. The WPF and MVVM side of things are going well.

However, the XAML and data binding side is a whole other story :)

How would I go about "disabling" a button?

For example, I have a CanClose property in my view model that determines whether or not the application can currently be closed. If a worker thread is off doing something, then this property is set to false and I'd like to either grey out the button or somehow visually disable the Close button via some sort of binding.

How would I go about doing this?

Thanks!

Edit -

Too bad I can only accept one answer.

These two answers helped me tremendously. In Kent's post, he went a step further by explaining why you should implement a command infrastructure in your application instead of disabling a button in the way that I had asked:

How does one "disable" a button in WPF using the MVVM pattern?

And the answer to my original question:

How does one "disable" a button in WPF using the MVVM pattern?

4条回答
【Aperson】
2楼-- · 2019-01-12 02:53

Just bind the IsEnabled property of the Button to CanClose:

<Button IsEnabled="{Binding CanClose}"/>
查看更多
虎瘦雄心在
3楼-- · 2019-01-12 02:58

By way of using the command pattern. In your view model:

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
    }
}

In your XAML:

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

Read this post to find out more about the DelegateCommand.

查看更多
贪生不怕死
4楼-- · 2019-01-12 03:01

If you return CanExecute of ICommand a value of false, then Button will be disabled. So whatever command your button is bound to, see if you can return CanExecute a value of false when you want to disable it.

查看更多
Summer. ? 凉城
5楼-- · 2019-01-12 03:01

This works too:

View:

        <Button>
            <Button.Style>
                <Style>
                    <Setter Property="Content" Value="Scream" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding btnEnabled}" Value="True">
                            <Setter Property="IsEnabled" Value="True" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

ViewModel:

    private bool _btnEnabled;
    public bool btnEnabled
    {
        get { return _btnEnabled; }
        set
        {
            if (_btnEnabled != value)
            {
                _btnEnabled = value;
                OnPropertyChanged();
            }
        }
    }
查看更多
登录 后发表回答