2

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? Commented Oct 11, 2013 at 9:34

1 Answer 1

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

is there another control that will take bindings for lists without having to roll my own?
No it's not because all the view controls are derived from ListViewbase class.
so everyone that wants to bind a list to a listview has to roll their own? Seems like MSFT should add that functionality to the control no? Oh well I will roll my own like you suggest and see where that goes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.