我有一个页面PeopleView,它包含一个显示People列表的GridView。我希望提供一种通过弹出式用户控件从People列表中添加删除的方法。 UserControl还允许搜索所有用户以添加更多内容。
当UserControl首次打开时,它应该显示它所做的所有人,并且应该已经在弹出的ListView中选择了当前在GridView中可见的任何人。当用户在UserControl中选择添加People时,应该添加People集合,并且主GridView会反映这一点。这一切都按预期工作。
当我尝试在弹出窗口中过滤人员列表时出现问题。这会导致FilteredPeople集合被重置,因此会从ListView中删除所有选择,从而从ObservableCollection中删除主页上的GridView。
我希望在保留选择的同时搜索人员,并在搜索时添加和删除此列表。
这是主页上的GridView:
<GridView x:Name="PeopleGridView"
Grid.Row="1"
Padding="116, 0, 0, 0"
ItemsSource="{Binding People}"
ItemTemplate="{StaticResource PeopleSelectItemTemplate}"
SelectionMode="Multiple" />
在此页面上是一个弹出式usercontrol,允许从此GridView添加和删除People。它还允许搜索人:
xaml看起来像这样:
<TextBlock Style="{StaticResource PopupHeaderTextStyle}"
Text="Enter person name:"
TextWrapping="Wrap"/>
<TextBox Grid.Column="1"
x:Name="SearchFieldTextBox"
TextChanged="SearchField_Changed"
Margin="20,0,20,0"
Height="30" />
<ListView Grid.Row="1"
Grid.ColumnSpan="2"
SelectedIndex="1"
SelectionMode="Multiple"
ItemsSource="{Binding FilteredPeople}"
extensions:ListViewExtensions.BindableSelection="{Binding People}"
ItemTemplate="{StaticResource PeopleSearchItemTemplate}"
HeaderTemplate="{StaticResource PeopleSearchHeaderTemplate}"/>
People和FilteredPeople是我的视图模型中的属性:
private ObservableCollection<PeopleViewModel> _people;
public ObservableCollection<PeopleViewModel> People
{
get { return people; }
set
{
if (Equals(people, value)) return;
people = value;
RaisePropertyChanged(() => People);
}
}
private IEnumerable<PeopleViewModel> _filteredPeople;
public IEnumerable<PeopleViewModel> FilteredPeople
{
get { return _filteredPeople; }
set
{
if (_filteredPeople == value) return;
_filteredPeople = value;
RaisePropertyChanged(() => FilteredPeople);
}
}
它们按负载填充:
FilteredPeople = _allPeople = _peopleService.GetAll();
People = _allPeople.Where(p => p.Selected);
当输入搜索文本时,将在viewmodel上调用FilterPeople方法,如下所示:
private void FilterPeople(string searchField)
{
FilteredPeople = _allPeople.Where(p => p.Name.Equals(searchField));
People = FilteredPeople.Where(p => p.Selected);
}
我正在使用WinRT XAML Toolkit提供的BindableSelection扩展。当选择ListView中的项目时,我更新基础模型对象:
void People_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
var person = e.NewItems[0] as PeopleViewModel;
person.Selected = true;
_personService.Save(person);
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
var person = e.OldItems[0] as PeopleViewModel;
person.Selected = false;
_personService.Save(person);
}
}
问题是FilteredPeople集合已重置,但我无法解决这个问题。我无法在网上找到任何可以做到这一点的例子。
我使用MVVM Cross就是这种情况下的任何用途
我做错了什么?