how to bind ItemTapped to ViewModel?

3 posts, 0 answers
  1. wil
    wil avatar
    11 posts
    Member since:
    Jul 2013

    Posted 17 Sep 2017 Link to this post

    I am using RadListView and trying to bind the ItemTapped to my MVVM.
    I can't seem to get this to work.  I can't find any examples online either.  

    Thanks in advance.
    Will

  2. Namysław
    Namysław avatar
    44 posts
    Member since:
    Jul 2017

    Posted 18 Sep 2017 Link to this post

    Your view code behind:
    ListxNameHere.Behaviors.Add(new EventWithArgsToCommand
    {
        EventName = nameof(List.ItemTapped),
        Command = vm?.SelectYourListItem,
        Converter = new ItemTapEventArgsToSelectedItemConverter()
    );


    Your view model:

    private RelayCommand<YourModel> _selectYourListItem;
    public RelayCommand<YourModel> SelectYourListItem => _selectYourListItem ?? (_selectYourListItem = new RelayCommand<YourModel>(async yourModel => await InvokeSelectYourListItem(yourModel)));

    Event to args converter:

    public class ItemTapEventArgsToSelectedItemConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var eventArgs = value as ItemTapEventArgs;
            return eventArgs?.Item;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
  3. Lance | Manager Technical Support
    Admin
    Lance | Manager Technical Support avatar
    1193 posts

    Posted 18 Sep 2017 Link to this post

    Hi Haluu,

    If you can't have any code-behind or just want all XAML, you can use an EventToCommandBehavior. Here's a good example.

    You'd still use Namyslaw's converter for the args, or you can just pass the whole args and manage it in the command..

    Regards,
    Lance | Tech Support Engineer, Sr.
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top