将ListView中的SelectedItems绑定到Windows Phone 8.1中的ViewModel

Sch*_*Box 4 c# xaml mvvm windows-phone-8.1

我有以下代码:

<ListView SelectionMode="Multiple" ItemsSource="{Binding MyList}" ItemTemplate="{StaticResource MyListTemplate}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
Run Code Online (Sandbox Code Playgroud)

使用以下DataTemplate:

<Page.Resources>
    <!-- Data Template for the ListView -->
    <DataTemplate x:Key="MyListTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Source="{Binding Path=Icon}" />
            <StackPanel Grid.Column="1" Orientation="Vertical">
                    <TextBlock Text="{Binding Path=EntryDate}" TextAlignment="Left" />
                <TextBlock Text="{Binding Path=Url}" TextAlignment="Left" />
                <TextBlock Text="{Binding Path=Text}" TextAlignment="Left" />
            </StackPanel>
        </Grid>
    </DataTemplate>
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)

在我的ViewModel中,我有以下内容:

private ObservableCollection<MyModel> myList;
public ObservableCollection<MyModel> MyList {
    get { return myList; }
    set {
        myList = value;
        RaisePropertyChanged("MyList");
    }
}

public IEnumerable<MyModel> SelectedItems {
    get { return MyList == null ? null : MyList.Where(e => e.IsSelected); }
}
Run Code Online (Sandbox Code Playgroud)

在我的模型中,我有其他人,我的IsSelected财产:

private bool isSelected;
public bool IsSelected {
    get { return isSelected; }
    set { Set(ref isSelected, value); }
}
Run Code Online (Sandbox Code Playgroud)

我可以看到它SelectedItems具有所有元素MyList,但是,当我在UI中选择一些元素时,属性IsSelected没有更新,它们都保持为假.
那么我在这里做错了什么?

Sch*_*Box 5

感谢MSDN论坛中的YossiStarz,我设法解决了我的问题.所以这是他的解决方案:

问题是你不能在你放置样式的元素上使用Style to SetBinding.这是因为在创建列表视图时创建样式一次,而不是为每个项容器创建样式.你实际上做了什么,是创建一个具有setter对象的样式对象,它的Value属性被限制为Style父级的DataContext的IsSelected(它没有).发生此绑定以设置setter中Value属性的值.如果它能成功获得价值,那么这就是它将设置给所有商品容器的价值.
我有一个解决方案给你.
首先是最简单的,创建这个帮助器类:

public class Helper {
    public static string GetIsSelectedContainerBinding(DependencyObject obj) {
        return (string)obj.GetValue(IsSelectedContainerBindingProperty);
    }

    public static void SetIsSelectedContainerBinding(DependencyObject obj, string value) {
        obj.SetValue(IsSelectedContainerBindingProperty, value);
    }

    // Using a DependencyProperty as the backing store for IsSelectedContainerBinding.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsSelectedContainerBindingProperty =
        DependencyProperty.RegisterAttached("IsSelectedContainerBinding", typeof(string), typeof(helper), new PropertyMetadata(null, IsSelectedContainerBindingPropertyChangedCallback));

    public static void IsSelectedContainerBindingPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        BindingOperations.SetBinding(d, ListViewItem.IsSelectedProperty, new Binding() {
            Source = d,
            Path = new PropertyPath("Content." + e.NewValue),
            Mode = BindingMode.TwoWay
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

现在将setter更改为:

<Style TargetType="ListViewItem">
    <Setter Property="local:Helper.IsSelectedContainerBinding" Value="IsSelected"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

这应该将SetBinding应用于创建的每个容器.