Xamarin.forms: Setting SelectedItem to null inside ItemSelected event handler does not work on UWP

Created on 26 Dec 2017  路  5Comments  路  Source: xamarin/Xamarin.Forms

Description

The standard pattern to disable highlighting of the selected row in a ListView is to set the SelectedItem property to null inside the ItemSelected event handler.

This worked fine on all platforms up to 2.4.0.280 but in 2.5.0 this no longer works for UWP. It works fine for Android and iOS

Steps to Reproduce

  1. Wire a ItemSelected event handler for a ListView
  2. Set the SelectedItem in the event handler to null

Expected Behavior

The row should not be selected

Actual Behavior

The row is selected for UWP apps.
It works fine on Android and iOS

Basic Information

  • Version with issue: 2.5.0.
  • Last known good version: 2.4.0.280

Reproduction Link

Test project is attached: App1.zip

Note: When not defining an event handler for ItemSelected, setting the SelectedItem to null works as expected. However, once the event is fired once, setting ItemSelected to null outside the event handler stops working as well.

1 excellent-report UWP bug

Most helpful comment

As a work around try Device.BeginInvokeOnMainThread(()=>ListView.SelectedItem = null);

All 5 comments

This used to work correctly in a previous release

As a work around try Device.BeginInvokeOnMainThread(()=>ListView.SelectedItem = null);

Probably related to #1133 or #1157.

using the Dispatcher workaround to set the SelectedItem to null works in my case.

Just a guess but... Xamarin doesn't handle binding to null at all. Unlike WPF. I would try setting the SelectedIndex property to -1. That should cause a trickle-down effect of making nothing selected in theListViewwith the result being thatSelectedItem` will eventually be set to null - IE: It would become the affect instead of the cause, of all the action.

Was this page helpful?
0 / 5 - 0 ratings