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

How to deselect RadListView item.

2 Answers 298 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Namek
Top achievements
Rank 1
Namek asked on 23 Aug 2016, 08:07 PM

I've started to use RadListView for my app.

My use case looks more or less:
- User has a ListView

- User can tap on any Item of ListView

- Some business logic is done at given Item

- User can tap again on any Item of ListView (even if it has been tapped just before)

For OOTB Xamarin.Forms ListView, I've just use command, binded to *SelectedItem* property of ListView, and after did some business logic, I've just pass *null* as SelectedItem (it informs ListView, that there is still no selection):

public MyItemObject SelectedItem
{
    get
    {
        return _selectedItem;
    }
    set
    {
        if (_selectedItem != value)
        {
            _selectedItem = value;
 
            // Some business logic here
 
            // Deselect item from ListView
            _selectedItem = null;
            RaisePropertyChanged();
        }
    }
}


However for *RadListView* it not working properly - it doesn't matter, if I use such "hacky" logic as for *ListView* or not, I had to tap 2 times, after my initial tap, so:
- User taps at item no. 1 - some logic is performed (OK)

- User taps again at item no. 1 - nothing is done, except "deselecting" *RadListView* selection (KO)

- User taps againt at item no. 1 - some logic is performed (OK)

Is there any possibility, to deselect *RadListView* from code behind?

Thanks a lot!

2 Answers, 1 is accepted

Sort by
0
Accepted
Vladislav
Telerik team
answered on 26 Aug 2016, 08:18 AM
Hi Namek,

If I understand you correctly you just need/want to execute some business logic on RadListViewItem tap regardless if it was previously selected or not. If this is the case can I suggest to use the RadListView's ItemTapped event which fires every time an item is tapped. Moreover if you don't need/want/use selection of items you can disable it by setting the SelectionMode to "None".

Please don't hesitate to contact us again if your scenario is different or I didn't understand it properly.

Regards,
Vladislav
Telerik by Progress
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
0
Namek
Top achievements
Rank 1
answered on 26 Aug 2016, 08:29 AM

Hi Vladislav,
Actually I've already achieved it exactly as you said:
1. Inside my XAML.CS code, I've implemented method, for handling event like:

private void OnItemTapped(object sender, ItemTapEventArgs e)
{
    if (e?.Item is MyItemObject)
    {
        MyViewModel.SelectedItem = (MyItemObject)e.Item;
    }
}

2. I've referenced my method, which handles even at XAML RadListView definition ItemTapped="OnItemTapped"
3. I've disabled selection of item at XAML RadListView definition SelectionMode="None"

In such case, I'm just replacing MyViewModel.SelectedItem object, against which I'm doing some business logic in viewmodel.
Tapping at given item, returning back and tapping again working as expected.

I tought that there might be more "beauty" solution (I don't like to have lot of code in code behind XAML), but at least it's working :).

Hope it might help someone else.

Thanks a lot.

Tags
ListView
Asked by
Namek
Top achievements
Rank 1
Answers by
Vladislav
Telerik team
Namek
Top achievements
Rank 1
Share this question
or