我有以下代码: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
基础教程推荐
- C#类和结构详解 2023-05-30
- unity实现动态排行榜 2023-04-27
- C# 调用WebService的方法 2023-03-09
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# List实现行转列的通用方案 2022-11-02
- C# windows语音识别与朗读实例 2023-04-27
- C#控制台实现飞行棋小游戏 2023-04-22
- 一个读写csv文件的C#类 2022-11-06
- ZooKeeper的安装及部署教程 2023-01-22
- winform把Office转成PDF文件 2023-06-14