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;
}
}
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.
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.
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With