Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何通过单击分组列表框的DataTemplate中的按钮从分组列表框中删除项目?_Wpf_Silverlight_Windows Store Apps_Windows Store - Fatal编程技术网

Wpf 如何通过单击分组列表框的DataTemplate中的按钮从分组列表框中删除项目?

Wpf 如何通过单击分组列表框的DataTemplate中的按钮从分组列表框中删除项目?,wpf,silverlight,windows-store-apps,windows-store,Wpf,Silverlight,Windows Store Apps,Windows Store,我在使用MVVM开发一个小型Windows应用商店应用程序时遇到了一个难题。我想用ListBox显示《老大哥》第14季中的一份房客名单。根据设计,每个ListBoxItem中都应该有一个按钮。单击按钮时,应触发一个命令,该命令将从ListBox中删除ListBoxItem 下面是我目前的解决方案。它可以工作,但不是很令人满意,因为每当从列表框中删除一个项时,模型都需要过滤整个集合并刷新整个列表框,这会导致一些性能问题,特别是当原始集合非常庞大时 [主页.xaml] <Page x:

我在使用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}">&#xE221;</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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Windows.Input;
使用Windows基金会;
使用Windows。
使用System.Collections.ObjectModel;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用Windows.UI.Popups;
使用系统诊断;
名称空间App1
{
公务舱房客
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串角色{get;set;}
公共bool已删除{get;set;}
}
公共类BigBrotherModel
{
公共可观察的集合房客{get;set;}
公共列表框lbx{get;set;}
public CollectionViewSource cvs{get;set;}
公共BigBrotherModel()
{
客房客人=新的可观察收集();
添加(newhousegest(){FirstName=“Ian”,LastName=“Terry”,Role=“Player”});
添加(newhousegest(){FirstName=“Shane”,LastName=“Meaney”,Role=“Player”});
添加(newhousegest(){FirstName=“Wil”,LastName=“Heuser”,Role=“Player”});
添加(newhousegest(){FirstName=“Danielle”,LastName=“Murphree”,Role=“Player”});
添加(newhousegest(){FirstName=“Jenn”,LastName=“Arroyo”,Role=“Player”});
添加(newhousegest(){FirstName=“Jodi”,LastName=“Rollins”,Role=“Player”});
添加(newhousegest(){FirstName=“Ashley”,LastName=“Iocco”,Role=“Player”});
添加(newhousegest(){FirstName=“Britney”,LastName=“Haynes”,Role=“Coach”});
添加(newhousegest(){FirstName=“Dan”,LastName=“Gheesling”,Role=“Coach”});
添加(newhousegest(){FirstName=“Janelle”,LastName=“Pierzina”,Role=“Coach”});
添加(newhousegest(){FirstName=“Mike”,LastName=“Boogie”,Role=“Coach”});
RemoveHouseGuestCommand=新的DelegateCommand(RemoveHouseGuest);
}
public ICommand RemoveHouseGuestCommand{get;set;}
void RemoveHouseGuest(对象参数)
{
Assert(param是housegest);
(参数为客房客人)。已删除=真;
刷新列表框();
}
对象GetGroupedView()
{
var view=来自客房内的hg
其中hg.Deleted==false
将汞按汞分组。角色分为g
orderby g.Key
选择新{Role=g.Key,Items=g};
返回视图;
}
公共空刷新列表框()
{
cvs.Source=GetGroupedView();
}
}
公共密封部分类主页面:第页
{
公共主页()
{
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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Windows.Input;
使用Windows基金会;
使用Windows。
使用System.Collections.ObjectModel;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用Windows.UI.Popups;
使用系统诊断;
使用System.Collections.Specialized;
名称空间App1
{
公务舱房客
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串角色{get;set;}
公共bool已删除{get;set;}
}
公共类客房组:iGroup
{
公共ObservableCollection项{get;set;}
公共字符串角色{get;set;}
公共住房宾客组()
{
Items=新的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();
}