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

我有以下代码:ListView SelectionMode=Multiple ItemsSource={Binding MyList} ItemTemplate={StaticResource MyListTemplate}ListView.ItemContainerStyleStyle TargetType=ListViewItemSetter Pro...

我有以下代码:

<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>

使用以下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>

在我的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); }
}

在我的模型中,我有其他人,我的IsSelected属性:

private bool isSelected;
public bool IsSelected {
    get { return isSelected; }
    set { Set(ref isSelected, value); }
}

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

解决方法:

多亏了YossiStarz in MSDN Forum,我设法解决了我的问题.所以这是他的解决方案:

The problem is that you cant use Style to SetBinding on the element
that you put the style on. This is because the style is created once
when the listview is created and not for each of the item containers.
what you actualy did, is creating a style object that have a setter
object that it’s Value property is bounded to the IsSelected of the
DataContext of the Style parent (witch it doesn’t have). This binding
occurs to set the value of the Value property in the setter. If it
would have succeed to get value, this was the value it would set to
all of the items containers.
I have a solution for you.
First
and the simplest, create this helper class:

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
        });
    }
}

Now change the setter to be like this:

<Style TargetType="ListViewItem">
    <Setter Property="local:Helper.IsSelectedContainerBinding" Value="IsSelected"/>
</Style>

This should apply SetBinding to each container that is created.

本文标题为:c# – 将ListView中的SelectedItems绑定到Windows Phone 8.1中的ViewModel

基础教程推荐