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

2024-04-25

我有以下代码:

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

使用以下数据模板:

<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没有更新,它们都仍然是假的。
那么我在这里做错了什么?


谢谢MSDN 论坛中的 YossiStarz https://social.msdn.microsoft.com/Forums/windowsapps/en-US/0304cdb4-320c-420f-8bcb-3e2d2367b1d9/binding-selecteditems-in-listview-to-a-viewmodel-in-windows-phone-81?forum=wpdevelop#dd8c2c79-4190-4cfb-943a-b50093cfb72b,我设法解决了我的问题。所以这是他的解决方案:

问题是您无法在元素上使用 Style 来 SetBinding 你把风格穿上了。这是因为样式创建一次 当创建列表视图时,而不是为每个项目容器创建。 您实际上所做的是创建一个具有设置器的样式对象 对象的 Value 属性绑定到 IsSelected Style 父级的 DataContext(但它没有)。这个绑定 发生在 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
        });
    }
}

现在将设置器更改为如下所示:

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 ListView 中的 SelectedItems 绑定到 Windows Phone 8.1 中的 ViewModel 的相关文章

随机推荐