This is a migrated thread and some comments may be shown as answers.

how to bind ItemTapped to ViewModel?

2 Answers 630 Views
ListView
This is a migrated thread and some comments may be shown as answers.
wil
Top achievements
Rank 1
wil asked on 18 Sep 2017, 03:15 AM
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 Answers, 1 is accepted

Sort by
0
Namysław
Top achievements
Rank 1
answered on 18 Sep 2017, 06:12 AM
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();
    }
}
0
Lance | Manager Technical Support
Telerik team
answered on 18 Sep 2017, 09:34 PM
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
Tags
ListView
Asked by
wil
Top achievements
Rank 1
Answers by
Namysław
Top achievements
Rank 1
Lance | Manager Technical Support
Telerik team
Share this question
or