Skip to content

Behaviors 1.1.3

Compare
Choose a tag to compare
@brianlagunas brianlagunas released this 28 Oct 16:05
f1aa06d

Change Log

  • #8: LaunchUriOrFileAction fails in .NET Core 3
  • #12: InvokeCommandAction - add additional properties for more MVVM scenarios
  • #13: .NET Core 3 Support
  • #29: Suggest to change XmlnsPrefix casing to lowercase. (Change XamlPrefix to "b")

Additional Info

InvokeCommandAction - add additional properties for more MVVM scenarios

Properties Added:

  • EventArgsConverter: Instance of IValueConverter that converts the EventArgs into a more MVVM friendly value
  • EventArgsConverterParameter: the parameter that will be sent as the parameter argument to IValueConverter.Convert method
  • EventArgsParameterPath: parameter path to extract a property from the EventArgs that will be passed to ICommand
  • PassEventArgsToCommand: forces the EventArgs to be passed to the ICommand if the other properties are null

EventArgsConverter

Using the EventArgsConverter to retrieve the ItemTappedEventArgs.Item property.

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();
        }
    }

In XAML, add a reference to the converter and the converter resource need to be defined

<b:InvokeCommandAction Command="{Binding ItemTappedCommand}"
                        EventArgsConverter="{StaticResource itemTappedEventArgsConverter}" />

EventArgsParameterPath

Sample EventArgs

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

<b:InvokeCommandAction Command="{Binding ItemTappedCommand}"
                                         EventArgsParameterPath="Item" />

Nested properties are also supported:

<b:InvokeCommandAction Command="{Binding ItemTappedCommand}"
                                         EventArgsParameterPath="Item.SubItem.SubProperty" />