There seems to be no way to get an initial selection to be show in a Xamarin Forms ListView. I see that this question has been asked a few times, however in each case there is either no relevant answer or the unanswered question is too complicated to be analyzed properly or the question is answered but the answer does not work. So I thought I would pose a simplified version of this question in the hope that someone can show me a workaround. A workaround is all that can be done because it has become clear to be that the SelectedItem cannot be bound and it cannot be set from code. Here are the SO question I have examined...
:Why can't I set the SelectedItem property of Xamarin.Forms.ListView?
Xamarin Forms ListView SelectedItem Binding Issue
Xamarin.Forms ListView set SelectedItem by Code
Xamarin XAML ListView - How to select programatically
Here is the XAML...
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XamarinFormsBench"
x:Class="XamarinFormsBench.ListViewPage1">
<ContentPage.BindingContext>
<local:ViewModel1/>
</ContentPage.BindingContext>
<ListView x:Name="MyListView" Margin="0,20,0,0"
ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
BindingContextChanged="MyListView_BindingContextChanged">
</ListView>
</ContentPage>
Here is the code...
public class Item
{
public Item(string name, string description)
{
Name = name;
Description = description;
}
string Name { get; set; }
string Description { get; set; }
public override string ToString()
{
return Name + " = " + Description;
}
}
public class ViewModel1 : INotifyPropertyChanged
{
public ObservableCollection<Item> Items { get; set; }
public ViewModel1()
{
Items = new ObservableCollection<Item>
{
new Item("Item 1","First item"),
new Item("Item 2","Second item"),
new Item("Item 3","Third item"),
new Item("Item 4","Fourth item"),
new Item("Item 5","Fifth item")
};
_selectedItem = Items[2];
}
Item _selectedItem;
public event PropertyChangedEventHandler PropertyChanged;
public Item SelectedItem
{
get
{
return _selectedItem;
}
set
{
_selectedItem = value;
NotifyPropertyChanged(nameof(SelectedItem));
}
}
public void NotifyPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ListViewPage1 : ContentPage
{
public ListViewPage1()
{
InitializeComponent();
}
public ViewModel1 ViewModel
{
get
{
return BindingContext as ViewModel1;
}
}
private void MyListView_BindingContextChanged(object sender, EventArgs e)
{
// MyListView.SelectedItem = ViewModel.SelectedItem;
}
}
My expectation is that a list of 5 items appears and the 3rd item will be highlighted. What happens is that 5 items appears but no item is highlighted. The binding clearly works because the selected item is updated each time I click on an item.
Now if I remove the binding and try to set the selected item in code by uncommenting the line near the end, that does not work either.
So I am out of ideas. I've been looking at simulating a mouse click but this is really a kluge beyond where I am willing to go.
I have run this code on UWP and Android. Haven't tried iOS.
