【发布时间】:2014-12-08 08:41:30
【问题描述】:
我有以下代码:
<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 属性没有更新,它们都保持为 false。
那么我在这里做错了什么?
【问题讨论】:
-
你能显示
MyListTemplate吗? -
问题已使用 DataTemplate 更新。
-
您是否在
IsSelected的设置器上设置了断点以检查单击项目时是否调用它? -
仅在我第一次填充集合时设置。当我选择/取消选择时,该集合没有被触发,但我不知道为什么。
-
奇怪,我刚刚在 WPF 中测试过它,它正在工作。无法在 WP8.1 上测试,因为我没有它
标签: c# xaml mvvm windows-phone-8.1