I'm having an issue with binding a Command to a button, with a Boolean CommandParameter. The result is always a disabled/not clickable button.
In WPF with the relayCommand from Microsoft.Toolkit.Mvvm. https://learn.microsoft.com/en-us/dotnet/api/microsoft.toolkit.mvvm.input.RelayCommand
the code: XAML:
...
xmlns:s="clr-namespace:System;assembly=mscorlib"
...
<s:Boolean x:Key="TrueValue">True</s:Boolean>
...
<Button
Content="Bool cmd"
Command="{Binding BoolCommand}"
CommandParameter="{StaticResource TrueValue}">
<!--CommandParameter="{Binding SomeBoolProperty}">-->
<!--<Button.CommandParameter>
<s:Boolean>
False
</s:Boolean>
</Button.CommandParameter>-->
</Button>
<Button
Content="String cmd"
Command="{Binding StringCommand}"
CommandParameter="Test"/>
Behind:
private bool _someBoolProperty;
public bool SomeBoolProperty
{
get => _someBoolProperty;
set => SetProperty(ref _someBoolProperty, value);
}
private ICommand _boolCommand;
public ICommand BoolCommand { get => _boolCommand ?? (_boolCommand = new RelayCommand<bool>(SetBool, (bool x) => canExecute)); }
private void SetBool(bool state)
{
//...
}
private bool canExecute {
get => true;
}
private ICommand _stringCommand;
public ICommand StringCommand { get => _stringCommand ?? (_stringCommand = new RelayCommand<string>(SetString)); }
private void SetString(string state)
{
//...
}
Some extra info:
- the xaml is inside a UserControl inside a DataTemplate.
- breakpoint in the SetString function is hit, and has no issues.
- canExecute is not hit.
- 'Xaml Binding failures ' window shows no issues with this.
i did read and try (see the commended code) some variations of this answer: Boolean CommandParameter in XAML
But the button is always grayed out; disabled; not clickable. Any ideas on what causes this button to be disabled?