Setting the initial selected item when binding to a ListView's SelectedItem property
Asked Answered
T

3

6

I have a Xamarin.Forms xaml page in which I am using a ListView to allow the user to pick a single item out of a list. I have bound the ListView's SelectedItem property to a property on my ViewModel and this works fine. As soon as the user changes the selected item the property in my viewmodel updates as well.

However, even though I initially set the property in my ViewModel to one of the values from the list, when the page loads the ListView's SelectedItem property is null, which in turn sets the ViewModel property to null as well. What I need is the other direction, I want the ListView to initially select the item that i've set in the VM property.

I can hack together a solution by writing extra code in the code behind file to explicitly set the initial selected item, but this introduces additional properties and complexity and is quite ugly.

What is the correct way to set the initial selected item of a ListView who's selected item is bound to a viewmodel property?

-EDIT-

I was asked to provide the code that I'm using for my binding. It's very simple, standard:

<ListView x:Name="myList" ItemsSource="{Binding Documents}" SelectedItem="{Binding SelectedDocument}">

the view model that is set as the binding context for the listview is instantiated before the page is created and looks like this:

public class DocumentSelectViewModel : ViewModelBase
{
    private Document selectedDocument;

    public List<Document> Documents
    {
        get { return CachedData.DocumentList; }
    }

    public Document SelectedDocument
    {
        get { return selectedDocument; }
        set { SetProperty(ref selectedDocument, value); 
    }

    public DocumentSelectViewModel()
    {
        SelectedDocuement = CachedData.DocumentList.FirstOrDefault();
    }
}

SetProperty is a function which simply rasies the INotifyPropertyChanged event if the new value is different from the old one, classical binding code.

Tetrachloride answered 10/9, 2015 at 7:12 Comment(2)
You need to post the code that you are using for the binding.Lissa
I posted the code. As I mentioned, it's just a simple data binding, nothing out of the ordinary. That is why I didn't initially give any code. I just want to know how to set the default value of a twoway binding. Right now, the default is always what the control has for it's default, I need the other way around, keep the view model property default and apply it to the control...Tetrachloride
L
1

I am a little rusty on XAML but don't you need to make the binding two-way?

E.G.

{ Binding SelectedDocument, Mode=TwoWay }

As long as the SelectedDocument property change raises the INotifyPropertyChanged event then you should get the desired effect.

Lissa answered 10/9, 2015 at 12:1 Comment(1)
The default binding mode for the SelectedItem is already two-way. The only trouble is when the binding is initiated, the listview's empty selected item overrides the viewmodels populated item. After initialization, the binding works both ways. Clicking on the UI in the list changes the property, and setting the property from code does change the listview, just need a way to let the default value in the property stay in place on initial binding. it gets overriden to listview's default "null"Tetrachloride
N
0

If you replace

public DocumentSelectViewModel()
{
    SelectedDocument = CachedData.DocumentList.FirstOrDefault();
}

By

public DocumentSelectViewModel()
{
    SelectedDocument = Documents.FirstOrDefault();
}

Does it work for you ? I had a similar problem that has been resolved this way...

Namaqualand answered 10/7, 2019 at 21:2 Comment(0)
W
0

You can use ctor DocumentSelectViewModel for set initial value. Honestly I dont like to make some job in ctor block but Xamarin.... You dont need DocumentSelectViewModel method. It will work.

public DocumentSelectViewModel ()
{ 
   SelectedDocument = Documents[0]; //or any your desired.
}
Whereunto answered 21/7, 2021 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.