[英]Binding SelectedItems in ListView to a ViewModel in 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>
使用以下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
没有更新,他们都保持假的。
那么我在这里做错了什么?
感谢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
});
}
}
现在将setter更改为:
<Style TargetType="ListViewItem">
<Setter Property="local:Helper.IsSelectedContainerBinding" Value="IsSelected"/>
</Style>
这应该将SetBinding应用于创建的每个容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.