CommunityToolkit / MVVM-Samples

Sample repo for MVVM package
Other
1.11k stars 222 forks source link

RelayCommand disables the button #103

Open zoranivoclar opened 1 year ago

zoranivoclar commented 1 year ago

Creating a dynamic buttons in code behind makes the button disabled when the RelayCommand is used. Here is a example

        var removeButton = new Button()
        {
            Content = "X",
            Command = new RelayCommand<int>(RemoveEntry),
            CommandParameter = index,
        };

then afterwards adding this button to a StackPanel the property isEnabled is always false (no matter how it's bound).

There is a workaround solution make it work First object initialize the button with IsEnabled =true, without setting the command

        var removeButton = new Button()
        {
            Content = "X",
            IsEnabled = true,
            CommandParameter = index,
        };

afterwards just set the command

        removeButton.Command = new RelayCommand<int>(RemoveEntry);
PeskovV commented 1 year ago

Hi! I have the same issue

I think you should ask question here https://github.com/CommunityToolkit/dotnet