1
votes

I feel like I'm missing something obvious because this is so simple. Thanks in advance for the help. I'm struggling with binding the SelectedItem of a simple ListView in a Xamarin application. I'm testing on UWP and am using Prism's MVVM BindableBase base class. Here's what I'm experiencing:

  1. The page loads and nothing in the list is selected.
  2. I select an item in the list.
  3. The setter of SelectedGrade is called with a value of null.
  4. After that, selecting items does not cause the SelectedGrade setter to be called.

Here's the relevant XAML:

<ListView BackgroundColor="#7F7F7F"
          CachingStrategy="RecycleElement"
          IsPullToRefreshEnabled="True"
          IsRefreshing="{Binding IsBusy, Mode=OneWay}"
          ItemsSource="{Binding Grades, Mode=OneWay}"
          SelectedItem="{Binding SelectedGrade, Mode=TwoWay}"
          RefreshCommand="{Binding RefreshCommand}"
          RowHeight="50">

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout HorizontalOptions="FillAndExpand"
                             VerticalOptions="CenterAndExpand"
                             Orientation="Horizontal"
                             Padding="10">
                    <Label HorizontalOptions="FillAndExpand"
                           Text="{Binding Title}"
                           TextColor="#272832"/>
                    <Label HorizontalOptions="FillAndExpand"
                           Text="{Binding Score}"
                           TextColor="Aquamarine" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>                
</ListView>

Here's the ViewModel:

Grade _selectedGrade;
public Grade SelectedGrade
{
    get { return _selectedGrade; }
    set { SetProperty(ref _selectedGrade, value); } // this gets set to NULL the first time i select an item then never gets set again
} 

Edit: I've also added an ItemSelected event listener in the code-behind and it is not being fired after the initial item is selected. The one time it is fired, the SelectedItemChangedEventArgs reveal that the ListView's SelectedItem is null. I can also see at this time that the ListView's ItemsSource has a collection of three items in it, as I would expect. I'm quite confused as to why the ListView thinks the SelectedItem is null and why it is not broadcasting when the selected item changes.

1
Have you debugged it putting a breakpoint in the SelectedGrade get ? You should watch if the view "request" it - Juan Carlos Rodriguez
@JuanCarlosRodriguez, the getter only gets called once when the page loads and the SelectedGrade is null. Then the setter is called (with a null value for some reason) when an item is first selected. Then neither the getter or setter are called again, despite other items in the ListView being selected. - NSouth
I've also added an ItemSelected event handler to the code-behind and it is not firing after initial item selection. It seems the ListView is not broadcasting when the selected item changes. - NSouth

1 Answers

0
votes

It looks like this was a bug in Xamarin. I upgraded from Xamarin.Forms 3.0.0.550146 to 3.1.0.637273 and now it works.