Skip to content

Latest commit

 

History

History
46 lines (36 loc) · 1.67 KB

EventToCommandBehavior.md

File metadata and controls

46 lines (36 loc) · 1.67 KB

EventToCommandBehavior

Execute ICommand when an event fires in UI Controls.

For example, ItemTapped event of ListView does not provide a property to bind Command.
The following code executes ICommand when firing the Itemtapped event.

<ListView ItemsSource="{Binding Fruits}">
    <ListView.Behaviors>
        <behaviorsPack:EventToCommandBehavior
            EventName="ItemTapped"
            Command="{Binding SelectedFruitCommand}"/>
    </ListView.Behaviors>

You can also pass any property of EventArgs to the ICommand by specifying the event's properties.
ItemTappedEventArgs is fired when the ListView's ItemTapped event occurs. ItemTappedEventArgs has Item property that holds the tapped item. Therefore, by specifying EventArgsPropertyPath, it is possible to pass tapped objects to ICommand.

<ListView ItemsSource="{Binding Fruits}">
    <ListView.Behaviors>
        <behaviorsPack:EventToCommandBehavior
            EventName="ItemTapped"
            Command="{Binding SelectedFruitCommand}"
            EventArgsPropertyPath="Item"/>
    </ListView.Behaviors>
public ICommand SelectedFruitCommand => new Command<Fruit>(fruits =>
{
    ...
});

Related References

Sample Codes

EventToCommandPage.xaml
EventToCommandPageViewModel.cs