Click here to Skip to main content
15,891,847 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a button click event.

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


The xaml:

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


Now I am going to use MVVM pattern then get rid of code behind, what is the code? Suppose use icommand.
Posted
Comments
syed shanu 1-Dec-14 23:39pm    
Check this link Hope this will help you.
http://stackoverflow.com/questions/3476686/how-does-one-disable-a-button-in-wpf-using-the-mvvm-pattern

http://stackoverflow.com/questions/18372262/mvvm-enable-disable-button-with-a-converter

http://www.codeproject.com/Questions/179129/MVVM-Enable-Disable-button-via-a-PropertyChanged-e
[no name] 2-Dec-14 8:50am    
I checked these links before and I know I have to use Property changed event something like. But I am just not sure how exactly to apply it to my issue. I need detail code.
Sergey Alexandrovich Kryukov 2-Dec-14 0:26am    
There is no such thing as miracle: if you don't want to write code behind, your condition can be only as primitive as binding to some other property. Isn't that obvious? And what's your problem? Bind the property IsEnabled. To what? It's up to you.
—SA

1 solution

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




C#
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
    }
}
 
Share this answer
 
Comments
[no name] 2-Dec-14 8:47am    
I know that I have to bind a property. The thing is I don't know how to apply it with PropertyChanged event. I need more code inside DoSomething method.
Member 10889447 2-Dec-14 21:38pm    
Check this link Hope this will help you.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/3acf2b83-3382-401e-ac02-0dfbc7561043/mvvm-refreshing-buttons-enabledisable-state
[no name] 3-Dec-14 8:46am    
It won't help. I need the specific code for my case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900