2
votes

I'm able to bind to the ItemSource no problem.

What I'm unable to do is bring back the SelectedItems and have them show in the listView.

I'd like to be able to have the listView display the checked items if it is found in the assignedChores collection. What am I doing incorrectly?

<ListView x:Name="choreList" BorderBrush="White" BorderThickness="1"     
Margin="401,322,613,150" Grid.Row="1" DisplayMemberPath="Summary"     
ItemsSource="{Binding choreList, Mode=OneWay}"     
SelectedItem = "{Binding personSingle.assignedChores, Mode=TwoWay}"     
SelectionMode="Multiple" SelectionChanged="choreList_SelectionChanged"/>
1
Is personSingle.assignedChores a List<T> or an object? - Farhan Ghumra

1 Answers

4
votes

ListView has SelectedItems property which is type of IList<T> but it's read only, so you can't bind it. SelectedItem can be bound to an object not to the List<T>.

You have only option, that's you need to bind ListViewItem's IsSelected property with ViewModel's property.

public class MyListView : ListView
{
    protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
    {
        base.PrepareContainerForItemOverride(element, item);
        ListViewItem listItem = element as ListViewItem;           
        Binding binding = new Binding();
        binding.Mode = BindingMode.TwoWay;
        binding.Source = item;
        binding.Path = new PropertyPath("IsSelectedFromViewModel");
        listItem.SetBinding(ListViewItem.IsSelectedProperty, binding);
    }
}