MVVM を使用して小さな Windows ストア アプリを作成しているときに、難しい問題が発生しました。ListBox を使用して、ビッグ ブラザー シーズン 14 のゲストのリストを表示したいと考えています。設計上、各 ListBoxItem にボタンが必要です。ボタンがクリックされると、ListBox から ListBoxItem を削除するコマンドを起動する必要があります。
以下は私の現在の解決策です。リストボックスからアイテムが削除されるたびに、モデルはコレクション全体をフィルタリングしてリストボックス全体を更新する必要があるため、機能しますが、特に元のコレクションが非常に大きい場合、パフォーマンスの問題が発生するため、あまり満足のいくものではありません。
[メインページ.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();
}
}
}
私の質問は: MVVM を使用して ListBox 全体を更新する必要なく、グループ化された ListBox から ListBoxItem を削除する方法はありますか? 私は本当にここで立ち往生しています。任意の提案をいただければ幸いです。よろしくお願いします。
[編集] 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;
}
}
}
ところで、最終的にもっと良い解決策が出てくることを期待して、この質問を開いたままにしておく方が良いと思います.