Home > Net >  What the best way to use command binding under Microsoft.Toolkit.MVVM
What the best way to use command binding under Microsoft.Toolkit.MVVM

Time:01-13

I'm using Microsoft.Toolkit.MVVM, in doc we can use binding Button command like:

<Button
    Content="Click me!"
    Command="{Binding ButtonClickCommand}"/>

But for ListView, I have to write like with Behaviors, this code is to long:

<ListView>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListView>

So why can't use below code instead? This is simple, but not working.

<ListView SelectionChanged={Binding SelectionChangedCommand}/>

CodePudding user response:

the simple answer is the type of the object in button Command is ICommand and in the ListView the SelectionChanged is an event and not ICommand , this why you need to use eventTrigger which is an "addon"

if you look at it from MVVM perspective the button is here to bind a command the ListView has SelectedItems collection that you can bind to and not the event , but that's more of a choice you need to make (I think that your code looks fine once you are used to it)

  •  Tags:  
  • Related