使用MVVM处理小型Windows应用商店时遇到了一个棘手的问题。我想用ListBox显示来自Big Brother第14季的家庭客人名单。按照设计,每个ListBoxItem中都应该有一个按钮。单击该按钮时,应触发一个命令,该命令将从ListBox中删除ListBoxItem。
以下是我目前的解决方案。它工作,但不是很令人满意,因为每当从ListBox中删除一个项目时,模型需要过滤整个集合并刷新整个ListBox,这会导致一些性能问题,特别是当原始集合非常庞大时。
[MainPage.xaml中]
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<CollectionViewSource x:Name="groupInfo" IsSourceGrouped="true" ItemsPath="Items" />
<local:BigBrotherModel x:Name="BBModel" />
<DataTemplate x:Key="lvwItemTemp">
<StackPanel>
<Grid Height="30">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Padding="0" Foreground="Red" FontFamily="Segoe UI Symbol"
Command="{Binding DataContext.RemoveHouseGuestCommand, ElementName=lbxHouseGuests}"
CommandParameter="{Binding}"></Button>
<TextBlock Grid.Column="1" Text="{Binding FirstName}" HorizontalAlignment="Stretch" />
<TextBlock Grid.Column="2" Text="{Binding LastName}" HorizontalAlignment="Stretch" />
</Grid>
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<ListBox x:Name="lbxHouseGuests" DataContext="{StaticResource BBModel}" ItemsSource="{Binding Source={StaticResource groupInfo}}" ItemTemplate="{StaticResource lvwItemTemp}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="0" Background="DarkGray" HorizontalAlignment="Stretch">
<TextBlock Width="500" Text="{Binding Role}"/>
</Border>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
</Grid>
</Page>
[MainPage.cs]
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Popups;
using System.Diagnostics;
namespace App1
{
public class HouseGuest
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Role { get; set; }
public bool Deleted { get; set; }
}
public class BigBrotherModel
{
public ObservableCollection<HouseGuest> houseGuests { get; set; }
public ListBox lbx { get; set; }
public CollectionViewSource cvs { get; set; }
public BigBrotherModel()
{
houseGuests = new ObservableCollection<HouseGuest>();
houseGuests.Add(new HouseGuest() { FirstName = "Ian", LastName = "Terry", Role="Player" });
houseGuests.Add(new HouseGuest() { FirstName = "Shane", LastName = "Meaney", Role = "Player" });
houseGuests.Add(new HouseGuest() { FirstName = "Wil", LastName = "Heuser", Role = "Player" });
houseGuests.Add(new HouseGuest() { FirstName = "Danielle", LastName = "Murphree", Role = "Player" });
houseGuests.Add(new HouseGuest() { FirstName = "Jenn", LastName = "Arroyo", Role = "Player" });
houseGuests.Add(new HouseGuest() { FirstName = "Jodi", LastName = "Rollins", Role = "Player" });
houseGuests.Add(new HouseGuest() { FirstName = "Ashley", LastName = "Iocco", Role = "Player" });
houseGuests.Add(new HouseGuest() { FirstName = "Britney", LastName = "Haynes", Role = "Coach" });
houseGuests.Add(new HouseGuest() { FirstName = "Dan", LastName = "Gheesling", Role = "Coach"});
houseGuests.Add(new HouseGuest() { FirstName = "Janelle", LastName = "Pierzina", Role = "Coach" });
houseGuests.Add(new HouseGuest() { FirstName = "Mike", LastName = "Boogie", Role = "Coach"});
RemoveHouseGuestCommand = new DelegateCommand(RemoveHouseGuest);
}
public ICommand RemoveHouseGuestCommand { get; set; }
void RemoveHouseGuest(object param)
{
Debug.Assert(param is HouseGuest);
(param as HouseGuest).Deleted = true;
RefreshListBox();
}
object GetGroupedView()
{
var view = from hg in houseGuests
where hg.Deleted == false
group hg by hg.Role into g
orderby g.Key
select new { Role = g.Key, Items = g };
return view;
}
public void RefreshListBox()
{
cvs.Source = GetGroupedView();
}
}
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
BBModel.cvs = groupInfo;
BBModel.lbx = lbxHouseGuests;
BBModel.RefreshListBox();
}
}
}
我的问题是:有没有办法从分组的ListBox中删除ListBoxItem而无需使用MVVM刷新整个ListBox?我真的被困在这里了。任何建议将不胜感激。非常感谢提前。
[编辑] 感谢Nate的建议,我在MainPage.cs中重写了我的代码,效果很好。这是源代码。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Popups;
using System.Diagnostics;
using System.Collections.Specialized;
namespace App1
{
public class HouseGuest
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Role { get; set; }
public bool Deleted { get; set; }
}
public class HouseGuestGroup : IGrouping<string, HouseGuest>
{
public ObservableCollection<HouseGuest> Items { get; set; }
public string Role { get; set; }
public HouseGuestGroup()
{
Items = new ObservableCollection<HouseGuest>();
}
public string Key
{
get { return Role; }
}
public IEnumerator<HouseGuest> GetEnumerator()
{
return Items.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return Items.GetEnumerator();
}
}
public class BigBrotherModel : ObservableCollection<HouseGuestGroup>
{
public BigBrotherModel()
{
RemoveHouseGuestCommand = new DelegateCommand(RemoveHouseGuest);
}
public ICommand RemoveHouseGuestCommand { get; set; }
void RemoveHouseGuest(object param)
{
Debug.Assert(param is HouseGuest);
HouseGuest guest = param as HouseGuest;
foreach (var g in Items)
{
if (g.Role == guest.Role)
{
g.Items.Remove(guest);
break;
}
}
}
}
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
HouseGuestGroup guestGroup;
guestGroup = new HouseGuestGroup();
guestGroup.Role = "Coach";
guestGroup.Items.Add(new HouseGuest() { FirstName = "Britney", LastName = "Haynes", Role = "Coach" });
guestGroup.Items.Add(new HouseGuest() { FirstName = "Dan", LastName = "Gheesling", Role = "Coach" });
guestGroup.Items.Add(new HouseGuest() { FirstName = "Janelle", LastName = "Pierzina", Role = "Coach" });
guestGroup.Items.Add(new HouseGuest() { FirstName = "Mike", LastName = "Boogie", Role = "Coach" });
BBModel.Add(guestGroup);
guestGroup = new HouseGuestGroup();
guestGroup.Role = "Player";
guestGroup.Items.Add(new HouseGuest() { FirstName = "Ian", LastName = "Terry", Role = "Player" });
guestGroup.Items.Add(new HouseGuest() { FirstName = "Shane", LastName = "Meaney", Role = "Player" });
guestGroup.Items.Add(new HouseGuest() { FirstName = "Wil", LastName = "Heuser", Role = "Player" });
guestGroup.Items.Add(new HouseGuest() { FirstName = "Danielle", LastName = "Murphree", Role = "Player" });
guestGroup.Items.Add(new HouseGuest() { FirstName = "Jenn", LastName = "Arroyo", Role = "Player" });
guestGroup.Items.Add(new HouseGuest() { FirstName = "Jodi", LastName = "Rollins", Role = "Player" });
guestGroup.Items.Add(new HouseGuest() { FirstName = "Ashley", LastName = "Iocco", Role = "Player" });
BBModel.Add(guestGroup);
groupInfo.Source = BBModel;
}
}
}
顺便说一句,我认为最好把这个问题保持开放,希望最终可以找到更好的解决方案。
答案 0 :(得分:0)
发生的事情是LINQ语句每次都在创建一个新列表,因此当您影响可观察集合时,它不会被反映,因为它们是两个不同的列表。您需要做的是创建一个自定义Group对象,在其旁边有一个ObservableCollection来存储guest虚拟机。然后,您将必须创建这些组的ObservableCollection。然后,您可以对静态集合(或任何子集合)进行任何所需的更改,它将反映在您的视图中。
然后,您可以做一些简单的事情并直接指向给定的可观察集合,如
void RemoveHouseGuest(object param)
{
Debug.Assert(param is HouseGuest);
if(houseGuests.Contains(param as HouseGuest))
houseGuests.Remove(param);
//(param as HouseGuest).Deleted = true;
//RefreshListBox();
}
希望这有助于编码!