Ian GM
Ian GM

Reputation: 787

ListView SelectedItem not firing in MVVM app using Template 10 UWP

I have this XAML...

<ListView ItemsSource="{Binding AllRoundIDs}" Height="96"
          SelectedItem="{Binding AllRoundsSelectedItem, Mode=TwoWay}"
          SelectionMode="Single"
          >
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Key}" FontSize="30" Foreground="White" Padding="0,0,0,1" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And this code in my ViewModel...

    private ObservableCollection<KeyValuePairs> _AllRoundIDs;
    public ObservableCollection<KeyValuePairs> AllRoundIDs
    {
        get { return _AllRoundIDs; }
        private set { Set(ref _AllRoundIDs, value); }
    }
    private KeyValuePairs _AllRoundsSelectedItem;
    public KeyValuePairs AllRoundsSelectedItem
    {
        get { return _AllRoundsSelectedItem; }
        private set { Set(ref _AllRoundsSelectedItem, value); }
    }

The KeyValuePairs Class looks like this...

public class KeyValuePairs
{
    public string Key { get; set; }
    public string Value { get; set; }
}

When I run my app I get items in my ListView as expected. So I know the data binding is working.

What I do not get is any life when I click an item. The binding to the AllRoundsSelectedItem does not fire. This code works fine in a WPF app but I get nothing in UWP. What am I missing?

Thanks in advance.

Upvotes: 1

Views: 1834

Answers (1)

Philip C
Philip C

Reputation: 1847

Your AllRoundsSelectedItem property's setter is private, so the ListView can't access it. You'll probably find there's a binding error in your output to that effect.

As an aside, because the KeyValuePairs class represents a single pair, it should probably be named KeyValuePair.

Upvotes: 3

Related Questions