1

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?

Result

Le len
  • 63
  • 7
  • The second parameter for the relaycommand should be a Func that returns a boolean. maybe: _boolCommand = new RelayCommand(SetBool, (bool x) => return canExecute)); might fix it? – Gert Hermans Dec 23 '21 at 11:01
  • @GertHermans I turned the CanExecute into a function. i cant find a syntax that allows a return at that position. also: RelayCommand takes a Func while RelayCommand takes a predicate as second parameter. (i dont really know the difference tho) – Le len Dec 23 '21 at 11:23
  • A Func allows more flexibility in terms of parameter count and types and the returned type. A Predicate always returns a bool. A Predicate describes a delegate that takes a parameter of type int and returns a bool. This is equal to a Func. – BionicCode Dec 23 '21 at 12:14
  • There is no issue with your posted code. I mean you could simplify it e.g., `x => true` as CanExecute delegate. Also pay attention to a consistent naming scheme. The property should be named `CanExecute` with capital `C`. You should post a complete but minimal working example that we can review. The RelayCommand implementation could also be helpful. – BionicCode Dec 23 '21 at 12:15
  • Sorry, but your question is confiusing to me, your intial claim is you have problems with the button that has `... Boolean CommandParameter ..`, than you show as a button with the command `Command="{Binding BoolCommand}"` which tries to use a `boolean` as `CommandParamter` but than you start talking about a different command `breakpoint in the SetString function is hit, and has no issues.`, so where is your issue? – Rand Random Dec 23 '21 at 12:21
  • I have added a link to the RelayCommand docs. (from Microsoft.Toolkit.MVVM.Input) the reason for added the string in the example is to show similar code does work, and to prevent adding all of the source code. and to prevent questions like, do you have the correct datacontext. – Le len Dec 23 '21 at 12:51
  • I have just tried this in a new clean project, and it runs fine. (bool cmd button is enabeld) But my question remains. perhaps its better worded as 'Why is this `BoolCommand` not binding correctly'? maybe it is because its inside a datatemplate? Any feedback on how to describe this problem question better is welcome. or now i dont know what to edit or show that can help – Le len Dec 23 '21 at 13:18
  • Change `Content="Bool cmd"` to `Content="{Binding}"` what does the Button have as a content? If it isn't the type where your command is declared than your datacontext is wrong and you have to specify it. eg change the binding of the command to `Command="{Binding BoolCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}"` - https://stackoverflow.com/questions/84278/how-do-i-use-wpf-bindings-with-relativesource – Rand Random Dec 23 '21 at 14:53
  • 1
    Please anser to comments with @[UserName] so that the user gets notified. – Rand Random Dec 23 '21 at 14:54
  • 1
    @Lelen: Your issue is not reproducible based on the information you have provided. – mm8 Dec 23 '21 at 18:12
  • @Lelen i have the same issue, have you found a solution for it? – VidyaPuri May 11 '23 at 06:51

0 Answers0