How Do You Disable a Button Using MVVM and Silverlight?

I need to disable a button using MVVM and Silverlight.  What is the best approach for doing this?

Thanks,

Dan
danielolorenzAsked:
Who is Participating?
 
Vel EousResearch & Development ManagerCommented:
A possible approach below:

ViewModel
using System;
using System.ComponentModel;
using System.Windows.Input;

namespace SilverlightApplication1
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private RelayCommand m_ToggleButton1EnabledCommand;
        public ICommand ToggleButton1EnabledCommand
        {
            get
            {
                if (m_ToggleButton1EnabledCommand == null)
                {
                    m_ToggleButton1EnabledCommand = new RelayCommand(p => true, p => ToggleButton1Enabled());
                }
                return m_ToggleButton1EnabledCommand;
            }
        }

        private bool m_Button1EnabledState = true;
        public bool Button1EnabledState
        {
            get { return m_Button1EnabledState; }
            set
            {
                m_Button1EnabledState = value;
                OnPropertyChanged("Button1EnabledState");
            }
        }

        private void ToggleButton1Enabled()
        {
            Button1EnabledState = Button1EnabledState == false ? true : false;
        }
    }

    public class RelayCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        Predicate<Object> _canExecute = null;
        Action<Object> _executeAction = null;

        public RelayCommand(Predicate<Object> canExecute, Action<object> executeAction)
        {
            _canExecute = canExecute;
            _executeAction = executeAction;
        }
        public bool CanExecute(object parameter)
        {
            if (_canExecute != null)
                return _canExecute(parameter);
            return true;
        }

        public void UpdateCanExecuteState()
        {
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, new EventArgs());
        }

        public void Execute(object parameter)
        {
            if (_executeAction != null)
                _executeAction(parameter);
            UpdateCanExecuteState();
        }
    }
}

Open in new window


XAML
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" IsEnabled="{Binding Button1EnabledState}" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="93,12,0,0" Name="button2" VerticalAlignment="Top" Width="75" Command="{Binding ToggleButton1EnabledCommand}" />

Open in new window


I have excluded the surrounding XAML gubbins for brevity as it is irrelevant in this example.

button1 has its IsEnabled DependencyProperty bound to a public property in the ViewModel.  button2 has its Command property bound to an ICommand in the ViewModel.  When button2 is pressed, the Command property of button2 is triggered, which in this example is ToggleButton1EnabledCommand.
This method translates your button press into the actual action you want to perform by passing a message along to the ToggleButton1Enabled method which sets Button1EnabledState to either true or false depending on its current value.
0
 
apeterCommented:
If you want to do in MVVM, Tchuk1 has explained most of the things. Did you try that ? Are you stuck up with anything else ?
0
 
danielolorenzAuthor Commented:
Thanks
0
 
Vel EousResearch & Development ManagerCommented:
Hi,

#3; http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27844020.html#a38340394

A working code example and description of how the code works has been provided which fulfills the requirements of the question authors request.
0
 
danielolorenzAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.