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

前端 未结 5 1588
陌清茗
陌清茗 2020-12-03 10:33

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 sid

相关标签:
5条回答
  • 2020-12-03 10:58

    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();
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-03 11:07

    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.

    0 讨论(0)
  • 2020-12-03 11:10

    Change in ViewModel file:

    public bool IsButtonEnabled { get { return _isButtonEnabled; }

    set
    {
        if (_isButtonEnabled == value)
        {
            return;
        }
    
        _isButtonEnabled = value;
        OnPropertyChanged("IsButtonEnabled");
    }
    }
    
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    

    Changes in the XAML file for Button: IsEnabled="{Binding IsButtonEnabled}"

    0 讨论(0)
  • 2020-12-03 11:12

    Just bind the IsEnabled property of the Button to CanClose:

    <Button IsEnabled="{Binding CanClose}"/>
    
    0 讨论(0)
  • 2020-12-03 11:15

    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.

    0 讨论(0)
提交回复
热议问题