1

My project uses CommunityToolkit.Mvvm8.0.

I use the [RelayCommand] attributeto create a method to generate the command.

https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/overview

Why is Click working fine but OnSelectionChanged not working?

Code:

  <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
       
        <TextBlock Text="{Binding FirstName}"/>
        <Button Content="Click Me" Command="{Binding OnSelectionChangedCommand}"/>
        <Button Content="Click Me"  Command="{Binding ClickCommand}"/>
    </StackPanel>

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

 public partial class MainWindow : Window
  {
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
  }
    public partial class ViewModel : ObservableObject
        {
            [ObservableProperty]
            private string firstName = "Kevin";
    
            public ViewModel()
            {
            }
    
            [RelayCommand]
            private void OnSelectionChanged()
            {
                FirstName = "David";
            }
            [RelayCommand]
            private void Click()
            {
                FirstName = "David";
            }
        }
wpf
  • 103
  • 6
  • Is there any conflict with the source code? I have not found a reason to prove it. – wpf Dec 13 '22 at 09:42

2 Answers2

1

According to RelayCommand attribute, "On" at the head of method name will be removed from auto-generated command.

The generator will use the method name and append "Command" at the end, and it will strip the "On" prefix, if present.

Thus, the name of Command will be SelectionChangedCommand.

emoacht
  • 2,764
  • 1
  • 13
  • 24
  • Hi,@emoacht.I read this document, is there any explanation that ·SelectionChangedCommand· cannot be customized? Is SelectionChangedCommand already defined? Is it because of conflicting definitions? – wpf Dec 14 '22 at 09:58
  • It is the rule how the generator automatically names the command. I guess the developers thought "On" at the head is not necessary for the name of a command because a method which starts with "On" is usually a method to handle event. – emoacht Dec 14 '22 at 10:07
  • Hi,@emoacht. Thus, the name of Command will be SelectionChangedCommand. What makes it not work? – wpf Dec 15 '22 at 02:02
  • Apart from the naming of command, I don't know your actual issue. Perhaps you can start another question. – emoacht Dec 15 '22 at 02:06
  • Hi,@emoacht. The question of this thread is why doesn't this command work. – wpf Dec 15 '22 at 02:10
  • @wpf: Because there is no command property named OnSelectionChangedCommand. You bind to a non-existing property. – mm8 Dec 15 '22 at 20:56
0

When you decorate a method with the RelayCommandAttribute, an ICommand property is generated and the name of that generated property will be method name with "Command" appended at the end.

As the docs clearly says, the "On" prefix will also be stripped from the generated property name.

So your sample code works just fine if you simply remove the "On" part from the XAML markup as there is no generated command named "OnSelectionChangedCommand":

<Button Content="Click Me" Command="{Binding SelectionChangedCommand}"/>
mm8
  • 163,881
  • 10
  • 57
  • 88