在使用 MVVM 开发小型 Windows Store 应用程序时,我遇到了一个棘手的问题。我想使用 ListBox 显示来自 Big Brother 第 14 季的房客列表。按照设计,每个 ListBoxItem 中都应该有一个按钮。单击按钮时,应触发一个命令,该命令将从 ListBox 中删除 ListBoxItem。
以下是我目前的解决方案。它可以工作,但不是很令人满意,因为每当从 ListBox 中删除一个项目时,模型都需要过滤整个集合并刷新整个 ListBox,这会导致一些性能问题,尤其是当原始集合非常庞大时。
[主页.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>
[主页.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;
}
}
}
顺便说一句,我认为最好让这个问题保持开放,希望最终能出现一些更好的解决方案。