删除数据绑定的ListView WPF控件中的(所有)项目 [英] Removing (all) items in a databound ListView WPF control

查看:283
本文介绍了删除数据绑定的ListView WPF控件中的(所有)项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据绑定的ListView WPF控件(绑定到XML文件).我创建了一个"RemoveAll"按钮尝试清除我的列表.您如何去做呢?我看过的SDK WPF示例似乎没有涵盖这一点.

I have a databound ListView WPF control (bound to an XML file).   I have created a "RemoveAll" button to try to clear my list.  How do you go about doing this?  The SDK WPF samples I have looked at don't seem to cover this.

类似于Windows窗体的明显代码如下:

the obvious Windows-Forms like code looks like this:

公共 void OnRemoveAll( object 发件人 RoutedEventArgs e)
{
的listView.Items.Clear();
}

public void OnRemoveAll(object sender, RoutedEventArgs e)
{
    listView.Items.Clear();
}

,您将收到以下异常:

在使用ItemsSource时,该操作无效.而是使用ItemsControl.ItemsSource访问和修改元素.

所以...如何删除数据绑定的WPF ListView(或ListBox)中的一项或所有项目.

Friedrich

推荐答案

将ItemsControl绑定到数据时,您不能直接修改它的视图,您可以做的是更改绑定源,例如从绑定源中添加或删除项目,然后将相应地更改视图.

Sheva
When an ItemsControl is data bound, you cannot directly modify its view, what you can do is to change the binding source, for instance adding or removing items from the binding source, then the view will be changed accordingly.

Sheva


这篇关于删除数据绑定的ListView WPF控件中的(所有)项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆