• Home
  • Documentation
Show / Hide Table of Contents
  • Introduction
  • Getting Started
    • Download and Setup Prism
    • NuGet Packages
    • Productivity Tools
  • Commands
    • Commanding
    • Composite Commands
  • Dependency Injection
    • Getting Started
    • Registering Types
    • Platform Specific Services
    • Exception Handling
    • ContainerLocator
    • Adding a Custom Container
    • Appendix
  • Event Aggregator
  • ViewModelLocator
  • Modules
  • WPF / Uno
    • Introduction
    • Getting Started
    • Converting From Prism 7.x
    • Converting From Prism 6.x
    • View Composition
    • Region Navigation
      • About Navigation in Prism
      • Basic Region Navigation
      • View/ViewModel Participation
      • Navigating to Existing Views
      • Passing Parameters
      • Confirming Navigation
      • Controlling View Lifetime
      • Navigation Journal
    • Interactivity
      • Event To Command
    • Dialog Service
    • Advanced
      • Region Adapters
    • Legacy (Prism 6)
      • Introduction
      • Initializing
      • Managing-Dependencies
      • Modules
      • Implementing-MVVM
      • Advanced-MVVM
      • Composing-the-UI
      • Navigation
      • Communication
      • Deploying
      • Appendix-A-Glossary
      • Appendix-B-Patterns
      • Appendix-C-Prism-Library
      • Appendix-D-Extending-Prism
      • Appendix-E-Click-Once
  • .NET MAUI
    • Getting Started
    • Migrating from Prism.Forms
    • PrismAppBuilder
    • Dependency Injection
    • AppModel
      • IPageLifecycleAware
    • Behaviors
      • Introduction
      • BehaviorBase<T>
      • EventToCommandBehavior
      • PageBehaviorFactory
    • Dialogs
      • Getting Started
      • IPageDialogService
      • IDialogService
    • Navigation
      • Introduction
      • Page Navigation
      • NavigationBuilder
      • Understanding the INavigationResult
      • NavigationExceptions
      • Global Navigation Observer
      • XAML Navigation
    • Regions
      • Introduction
  • Xamarin.Forms
    • Create Your First App
    • Behaviors
      • Working with Behaviors
      • EventToCommand Behavior
      • PageBehaviorFactory
    • Dialogs
      • Dialogs
      • Page Dialog Service
      • Dialog Service
      • Styling Dialogs
    • Navigation
      • Navigation Basics
      • Passing Parameters
      • Confirming Navigation
      • Deep Linking
      • Working w/ MasterDetailPages
      • Working w/ NavigationPages
      • Working w/ TabbedPages
      • XAML Navigation
    • Application Lifecycle
    • Page Lifecycle
    • Additional Platforms
      • GTK

Using the EventToCommandBehavior

The EventToCommandBehavior class provides a convenient way to, in XAML, "bind" events to ICommand according to MVVM paradigm to avoid code behind.

Properties

The EventToCommandBehavior expose the following properties

  • EventName The name of the event to listen to. For example ItemTapped
  • Command The ICommand that will be executed when the event is raised
  • CommandParameter The parameter that will be sent to the ICommand.Execute(object) method
  • EventArgsConverter Instance of IValueConverter that allows operating on the EventArgs type for the EventName
  • EventArgsConverterParameter The parameter that will be sent as the parameter argument to IValueConverter.Convert method
  • EventArgsParameterPath Parameter path to extract property from EventArgs that will be passed to ICommand.Execute(object)

Usage

First declare the namespace and assembly in where EventToCommandBehavior is declared and declare a XML-namespace.

xmlns:prism="http://prismlibrary.com"

CommandParameter

Bind or declare a parameter that will be sent to the ICommand.Execute(object) method.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyNamespace.ContentPage"
             xmlns:prism="http://prismlibrary.com">
    <ListView>
        <ListView.Behaviors>
            <prism:EventToCommandBehavior EventName="ItemTapped"
                                          Command="{Binding ItemTappedCommand}"
                                          CommandParameter="MyParameter" />
        </ListView.Behaviors>
    </ListView>
</ContentPage>

EventArgsConverter

Using the EventArgsConverter to retrieve the ItemTappedEventArgs.Item property.

using System;
using System.Globalization;
using Xamarin.Forms;

namespace Prism.Converters
{
    public class ItemTappedEventArgsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var itemTappedEventArgs = value as ItemTappedEventArgs;
            if (itemTappedEventArgs == null)
            {
                throw new ArgumentException("Expected value to be of type ItemTappedEventArgs", nameof(value));
            }
            return itemTappedEventArgs.Item;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

The XAML need a reference to the converter and the converter resource need to be defined

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyNamespace.ContentPage"
             xmlns:prism="http://prismlibrary.com"
             xmlns:c="clr-namespace:AwesomeApp.Converters;assembly=YourProject">
    <ContentPage.Resources>
        <ResourceDictionary>
            <c:ItemTappedEventArgsConverter x:Key="itemTappedEventArgsConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <ListView>
        <ListView.Behaviors>
            <prism:EventToCommandBehavior EventName="ItemTapped"
                                          Command="{Binding ItemTappedCommand}"
                                          EventArgsConverter="{StaticResource itemTappedEventArgsConverter}" />
        </ListView.Behaviors>
    </ListView>
</ContentPage>

EventArgsParameterPath

Attach the command to ItemTapped event will raise the ItemTappedEventArgs event.

public class ItemTappedEventArgs : EventArgs
{
    public object Item { get; }
    public object Group { get; }
}

Setting EventArgsParameterPath to Item will extract the property value and pass it to the ICommand.Execute(object) method

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyNamespace.ContentPage"
             xmlns:prism="http://prismlibrary.com">
    <ListView>
        <ListView.Behaviors>
            <prism:EventToCommandBehavior EventName="ItemTapped"
                                          Command="{Binding ItemTappedCommand}"
                                          EventArgsParameterPath="Item" />
        </ListView.Behaviors>
    </ListView>
</ContentPage>
  • Edit on GitHub
  • Ask questions
  • Follow @PrismLib
  • Follow @BrianLagunas
  • Follow @DanJSiegel
Back to top Copyright 2015-2022 Prism