?
Solved

How Do You Disable a Button Using MVVM and Silverlight?

Posted on 2012-08-27
8
Medium Priority
?
610 Views
Last Modified: 2012-09-10
I need to disable a button using MVVM and Silverlight.  What is the best approach for doing this?

Thanks,

Dan
0
Comment
Question by:danielolorenz
  • 2
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
Vel Eous earned 2000 total points
ID: 38340394
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
 
LVL 25

Expert Comment

by:apeter
ID: 38342872
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
 

Author Comment

by:danielolorenz
ID: 38345953
Thanks
0
 
LVL 14

Expert Comment

by:Vel Eous
ID: 38357952
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
 

Author Comment

by:danielolorenz
ID: 38359833
Thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question