Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button IsEnabled binding not working properly

Tags:

c#

wpf

xaml

Button.IsEnabled doesn't work properly.

I have debugged the code and the setter for the property was hit with the "true" value. But the button is still disabled.

View.xaml:

<StackPanel Grid.Row="2" Margin="0,20,0,0" >
        <Button Name="ButtonOk" Content="OK" Margin="0,0,4,0" IsEnabled="{Binding SomethingIsValid}"  Command="{Binding CommandOk}" />
        <Button Name="ButtonCancel" Content="Cancel" Margin="0,0,4,0" IsCancel="True" /
</StackPanel>

View.xaml.cs:

...
public View(ViewModel viewModel)
{
    this.InitializeComponent();

    this.viewModel = viewModel;
    this.DataContext = viewModel;            
}

ViewModel:

public bool SomethingIsValid
{
   get
   {
      return somethingIsValid;
   }
   set
   {
      this.somethingIsValid= value;
      this.RaisePropertyChanged(() => this.SomethingIsValid);
   }
}

#region IDataErrorInfo
public string this[string columnName]
{
   get
   {
      this.SomethingIsValid= false;

      if ("SomeName" == columnName)
      {
         if (string.IsNullOrEmpty(this.Status))
         {
            return "Please bla bla..";
         }
      }

      this.SomethingIsValid = true;
      return string.Empty;
   }
}

public string Error
{
   get
   {
      return string.Empty;
   }
}
#endregion

public ICommand CommandOk
{
   get
   {
      if (this.commandOk == null)
      {
         this.commandOk = new RelayCommand(this.CommandOkAktion, () => this.SomethingIsValid );
      }

      return this.commandOk;
   }
}
like image 423
MikroDel Avatar asked Mar 05 '15 09:03

MikroDel


4 Answers

If anyone is still having trouble with this - make sure that your IsEnabled property comes after the Command property.

<Button Command="{Binding StartCommand}" IsEnabled="{Binding StartEnabled}" />

- works

  <Button IsEnabled="{Binding StopEnabled}" Command="{Binding StopCommand}"  />

- does not work

Note that both of my commands can always execute. An easier fix for me.

like image 155
Anik Saha Avatar answered Oct 19 '22 07:10

Anik Saha


If you are using a command, it's not a good idea to separately bind the IsEnabled property of the button. Instead you should provide the correct value in the "CanExecute" of the command. This will enable the button as well.

like image 31
Martin Moser Avatar answered Oct 19 '22 07:10

Martin Moser


Form what I can see in your posted code, if nothing calls this["SomeName"], your SomethingIsValid will always be false, and that is why your button is showing disabled. So my suggestion/solution is:

Remove IsEnabled binding from xaml (as you won't need this if your Command binding is working correctly):

<StackPanel Grid.Row="2" Margin="0,20,0,0" >
    <Button Name="ButtonOk" Content="OK" Margin="0,0,4,0" Command="{Binding CommandOk}" />
    <Button Name="ButtonCancel" Content="Cancel" Margin="0,0,4,0" IsCancel="True" /
 </StackPanel> 

Change your ViewModel code to something like below (your current Command definition is OK, so no change to that part). The code below makes sure when executing CanCommandExecute, it will re-examine SomethingIsValid:

public bool SomethingIsValid
{
   get
   {
      return string.IsNullOrEmpty(Error);
   }
}

public string this[string columnName]
{
   get
   {
      switch(columnName)
      {
          case "SomeName":
          {
               if (string.IsNullOrEmpty(this.Status))
               {
                  return "Please bla bla..";
               }
               break;
          }
          case "SomeOtherName":
          {
               if (string.IsNullOrEmpty(this.OtherProperty))
               {
                  return "Please bla bla..";
               }
               break;
          }

      }
      return string.Empty;         

   }
}

public string Error
{
   get
   {
      return this["SomeName"]+this["SomeOtherName"];
   }
}
#endregion
like image 1
Bolu Avatar answered Oct 19 '22 07:10

Bolu


If you are using RelayCommand, you can use the second parameter as boolean method which indicates whether command can be executed:

public RelayCommand(Action execute, Func<bool> canExecute);

Usage:

this.DetermineCommand = new RelayCommand(this.Determine, () => this.IsValid);
like image 1
Lenka Avatar answered Oct 19 '22 07:10

Lenka