0
votes

I am not sure if this is an MVVM-Light bug or just something that doesn't work in WPF.... I have a button where I am setting the Command property to a RelayCommand. Everything works fine as long as I don't set the CommandParameter property. Once I do, the CanExecute callback quits working.

I am using the GalaSoft.MvvmLight.CommandWpf namespace as I am working in .Net 4.5.2. Here is a code snippit:

    public RelayCommand<IList> SetFlagCommand { get; private set; }
    ...
    SetFlagCommand = new RelayCommand<IList>(SetFlag, CanSetFlag);
    ...
     mButtons.Add(new Button
        {
            ...
            Command = SetFlagCommand,
            CommandParameter = new Binding("SelectedItems") { ElementName = "lstAllChoices" },
            });

    ...
    private void SetFlag(IList list)...
    private bool CanSetFlag(IList list)...

The CanSetFlag method is called once, but then never again. If I don't set the CommandParameter property, it works as expected. Any ideas what is going on?

Thanks for any help!

1

1 Answers

1
votes

You are not supposed to set the CommandParameter property to a Binding. Bind it using the BindingOperations.SetBinding method instead:

Button button = new Button()
{
    Command = SetFlagCommand
};
BindingOperations.SetBinding(button, Button.CommandParameterProperty, new Binding("SelectedItems") { ElementName = "lstAllChoices" });

mButtons.Add(btn);

Also make sure that the type of the source property (SelectedItems) matches the type parameter T of the RelayCommand<T>.