WPF删除列表框项

时间:2011-10-26 20:29:30

标签: wpf

我向用户显示了一系列项目。

双击特定项目后,我想从列表中删除此项目。

我这样做是因为我不喜欢这个修改数据源而不仅仅是列表中的项目。此外,我意识到,如果我有大约1000条记录,刷新将会很慢。

这是从列表框中删除项目的更好方法吗? 我不想修改数据源,因为我也会将它用于历史记录。

虚拟数据设置:

    private void SetUpData()
    {
        this.users = new List<UserNames>();
        this.users.Add(new UserNames() {Id = 1, UserName = "name 1"});
        this.users.Add(new UserNames() { Id = 2, UserName = "name 2" });
        this.users.Add(new UserNames() { Id = 3, UserName = "name 3" });
        this.users.Add(new UserNames() { Id = 4, UserName = "name 4" });
        this.users.Add(new UserNames() { Id = 5, UserName = "name 5" });
        this.users.Add(new UserNames() { Id = 6, UserName = "name 6" });
        this.users.Add(new UserNames() { Id = 7, UserName = "name 7" });
        this.users.Add(new UserNames() { Id = 8, UserName = "name 8" });
        this.users.Add(new UserNames() { Id = 9, UserName = "name 9" });
        this.users.Add(new UserNames() { Id = 10, UserName = "name 10" });
        this.listBox1.DataContext = users;
    }

我的方法是从列表中删除该项目。

    private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        UserNames itemToRemove = (UserNames)((ListBox)sender).SelectedItem;
        MessageBox.Show(itemToRemove.UserName);
        this.listBox1.DataContext = null;

        this.users.Remove(itemToRemove); // remove from list
        this.listBox1.DataContext = this.users; // update data source
    }

我的列表框定义:

<ListBox Grid.Column="1" Grid.Row="1" Name="listBox1" ItemsSource="{Binding}" SelectedValuePath="Id" MouseDoubleClick="listBox1_MouseDoubleClick">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding UserName}"  />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

4 个答案:

答案 0 :(得分:1)

更好地使用MVVM方法,并将listbox.itemssource绑定到property类型observableCollection<UserNames>

这样,通过从observableCollection中删除项目,该项目将从listbox中移除,并且UI将自动更新。

此示例显示PersonList binding to a listbox,它对您有用。

答案 1 :(得分:1)

MVVM是推荐的模式,但对于您的问题的答案不是必需的。

  1. 实施INotifyPropertyChanged
  2. 更改您的列表&lt;&gt;到ObservableCollection&lt;&gt;如上所述
  3. 关于刷新速度慢的另一个问题 - 您还可以使用设置列表框来使用虚拟化,这样只会绘制屏幕上的项而不是整个框。

答案 2 :(得分:0)

建议使用mvvm,但这应该对您有用:

private void SetUpData()//call this just once
{
    this.users = new ObservableCollection<UserNames>();
    this.users.Add(new UserNames() {Id = 1, UserName = "name 1"});
    this.users.Add(new UserNames() { Id = 2, UserName = "name 2" });
    this.users.Add(new UserNames() { Id = 3, UserName = "name 3" });
    this.users.Add(new UserNames() { Id = 4, UserName = "name 4" });
    this.users.Add(new UserNames() { Id = 5, UserName = "name 5" });
    this.users.Add(new UserNames() { Id = 6, UserName = "name 6" });
    this.users.Add(new UserNames() { Id = 7, UserName = "name 7" });
    this.users.Add(new UserNames() { Id = 8, UserName = "name 8" });
    this.users.Add(new UserNames() { Id = 9, UserName = "name 9" });
    this.users.Add(new UserNames() { Id = 10, UserName = "name 10" });
    this.listBox1.DataContext = users;
}


private void listBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    UserNames itemToRemove = (UserNames)((ListBox)sender).SelectedItem;
    MessageBox.Show(itemToRemove.UserName);

    this.users.Remove(itemToRemove); // remove from list
}

答案 3 :(得分:0)

如何不更改集合但删除显示的项目的方法如下:

IEditableCollectionView items = tabControl.Items; //Cast to interface
if (items.CanRemove)
{
    items.Remove(tabControl.SelectedItem);
}

谢谢你的帮助。 我在这里找到了它:WPF: Remove dotted border around focused item in styled listbox