Skip to main content
A newer version of this page is available. .

EventToCommand.EventArgsConverter Property

Gets or sets an object that converts the event’s argument to a command’s parameter. This is a dependency property.

Namespace: DevExpress.WinUI.Core

Assembly: DevExpress.WinUI.Core.v22.1.dll

NuGet Package: DevExpress.WinUI

Declaration

[DP(null)]
public IEventArgsConverter EventArgsConverter { get; set; }

Property Value

Type Description
IEventArgsConverter

An object that implements the IEventArgsConverter interface.

Remarks

The EventToCommand behavior allows you to pass an event’s arguments to a command. If you want to maintain a clean MVVM pattern and convert the event’s arguments to an object suitable for the command, specify the EventArgsConverter property as follows:

<ListBox ...>
    <dx:Interaction.Behaviors>
        <dx:EventToCommand EventName="DoubleTapped" Command="{x:Bind EditCommand}">
            <dx:EventToCommand.EventArgsConverter>
                <local:CustomEventArgsConverter/>
            </dx:EventToCommand.EventArgsConverter>
        </dx:EventToCommand>
    </dx:Interaction.Behaviors>
</ListBox>

The defined EventArgsConverter should implement the IEventArgsConverter interface. You can also derive the converter from the EventArgsConverterBase<TArgs, TResult> class, which already implements the IEventArgsConverter interface.

public interface IEventArgsConverter {
    object Convert(object sender, object args);
    void ConvertBack(object sender, object args, object parameter);
}
public abstract class EventArgsConverterBase<TArgs, TResult> : IEventArgsConverter {
    protected abstract TResult Convert(object sender, TArgs args);
    protected virtual void ConvertBack(object sender, TArgs args, TResult parameter) { }
}

When you implement the converter, you can use the LayoutTreeHelper class that can search for nodes in an application’s visual tree:

using DevExpress.WinUI.Core;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.Linq;

public class ListBoxPointerEventArgsConverter : EventArgsConverterBase<RoutedEventArgs, SelectedItemInfo> {
    protected override SelectedItemInfo Convert(object sender, RoutedEventArgs args) {
        var listBox = (ListBox)sender;
        var originalSource = (DependencyObject)args.OriginalSource;
        var listBoxItem = LayoutTreeHelper.GetVisualParents(originalSource, listBox)
            .OfType<ListBoxItem>()
            .FirstOrDefault();
        var selectedItem = listBoxItem != null ? listBox.ItemFromContainer(listBoxItem) : null;
        return new SelectedItemInfo(selectedItem);
    }
}
public class SelectedItemInfo {
    public object Value { get; }
    public SelectedItemInfo(object value) {
        Value = value;
    }
}
See Also