2

我能够绑定到 ItemSource 没问题。

我无法做的是带回 SelectedItems 并让它们显示在 listView 中。

如果在assignedChores 集合中找到它,我希望能够让listView 显示选中的项目。我做错了什么?

<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"/>
4

1 回答 1

4

ListViewSelectedItems类型的属性,IList<T>但它是只读的,所以你不能绑定它。SelectedItem可以绑定到一个objectnot to the List<T>

你只有一个选择,那就是你需要绑定ViewModelListViewItemIsSelected属性。

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);
    }
}
于 2013-10-11T10:01:32.103 回答