None
None

Reputation: 5670

SelectedItems ListView binding fails in UWP

I have a ListView like this

<ListView x:Name="lview" SelectionMode="Multiple" SelectedItems="{x:Bind SelectedItems}">

</ListView>

with this code-behind:

public IList<object> SelectedItems
{
    get => (IList<object>)GetValue(SelectedItemsProperty);
    set => SetValue(SelectedItemsProperty, value);
}

public static readonly DependencyProperty SelectedItemsProperty =
    DependencyProperty.Register("SelectedItems", typeof(IList<object>), typeof(MainPage),
        new PropertyMetadata(null));

public MainPage()
{
    this.InitializeComponent();
    List<int> io = new List<int> { 111111, 222, 33333, 4444444 };
    lview.ItemsSource = io;
}

Here I am trying to bind a dependency property (SelectedItems) to my listview. But this gives me an error

Error XDG0013 The property "SelectedItems" does not have an accessible setter.

What am I doing wrong here? Is it not possible to bind SelectedItems to a dependency property?

Upvotes: 1

Views: 550

Answers (1)

Corentin Pane
Corentin Pane

Reputation: 4943

The SelectedItems property of a ListView is read-only as documented here, hence prohibiting two way binding in UWP.

However, you could bind each item IsSelected property to the viewmodel and get the collection of selected items by filtering only items with IsSelected to true.

Upvotes: 3

Related Questions