Bug 47712 - ListView highlighting is reflected from SelectedValue
Summary: ListView highlighting is reflected from SelectedValue
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.1
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-11-23 13:22 UTC by Dominik Rupp
Modified: 2017-02-27 12:48 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Dominik Rupp 2016-11-23 13:22:36 UTC
Create a ListView and the SelectedItem binding-mode 2 way.

When setting the SelectedItem from the view-model the changes are reflected in the view, however the selected row is not highlighted. Of course, INotifyPropertyChanged is implemented.


Code to reproduce:
View:
<ListView ItemsSource="{Binding List}" SelectedItem="{Binding Selected, Mode=TwoWay}">...</ListView>

VM:
ctor() 
{
    Selected = List.First();
}

public Type Selected
{
    get { return _selected; }
    set { SetProperty(ref _selected, value); }
}

Tested for Android only.
Comment 1 adrianknight89 2016-11-23 16:07:23 UTC
I don't have this issue. Are you using an ObservableCollection for your list?

ViewModel:

public class ViewModel : INotifyPropertyChanged
	{
		public event PropertyChangedEventHandler PropertyChanged;

		protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
		{
			if (Equals(storage, value))
			{
				return false;
			}

			storage = value;
			OnPropertyChanged(propertyName);
			return true;
		}

		protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
		{
			PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
		}

		ObservableCollection<string> names;
		public ObservableCollection<string> Names
		{
			get { return names; }
			set { SetProperty(ref names, value); }
		}

		string selected;
		public string Selected
		{
			get { return selected; }
			set { SetProperty(ref selected, value); }
		}

		public ViewModel()
		{
			Names = new ObservableCollection<string>();

			for(var i=0; i<50; i++)
				Names.Add(i.ToString());

			Selected = Names.First();
		}
	}

View:

<ContentPage.BindingContext>
		<local:ViewModel></local:ViewModel>
	</ContentPage.BindingContext>

	<ListView ItemsSource="{Binding Names}" SelectedItem="{Binding Selected, Mode=TwoWay}">
		
	</ListView>
Comment 2 Rui Marinho 2017-02-20 16:16:30 UTC
Thank you for taking the time to submit the bug. 
We tried to reproduce the issue you reported but were unable given the description. If you could please attach a reproduction to the bug by starting with a clean Xamarin.Forms project and adding just the code necessary to demonstrate the issue, we would very much appreciate it.  
 
For your convenience, we have created some reproduction best practices viewable here: https://gist.github.com/jassmith/92405c300e54a01dcc6d  

Warm regards, 
Xamarin Forms Team
Comment 3 Dominik Rupp 2017-02-27 12:48:49 UTC
This has nothing to do with ObservableCollection @adrianknight.

I just retried with XF 2.3.3.175 and cannot reproduce the problem.