绑定到 ObjectDataProvider 时如何删除 ListView 中的所有项目
我将 ListView 绑定到 ObjectDataProvider。我从用户那里获取一些值,并在运行时更改我的 ObjectDataProvider,但是当我的 ObjectDataProvider 更新了所有项目时,将其添加到 ListView 并替换它们。我使用此语句:
lstUsers.Items.Clear();
但出现此错误:
当 ItemsSource 正在使用时,操作无效。使用 ItemsControl.ItemsSource 访问和修改元素。
当列表视图绑定到 ObjectDataProvider 时,如何从列表视图中删除所有数据?
谢谢
编辑1): 这是我的代码:
public partial class Page_ObjectDataProvider : Window
{
public Page_ObjectDataProvider()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
int myValue =10;
((ObjectDataProvider)this.FindResource("ADUsers")).MethodParameters.Clear();
((ObjectDataProvider)this.FindResource("ADUsers")).MethodParameters.Add(myValue);
((ObjectDataProvider)this.FindResource("ADUsers")).Refresh();
}
}
public class CustomData
{
public int F1 { get; set; }
public int F2 { get; set; }
public string F3 { get; set; }
}
public class RetrievCustomData : List<CustomData>
{
public RetrievCustomData GetSome(int i)
{
for (int j = 0; j < i; j++)
{
CustomData cd = new CustomData();
Random rnd = new Random();
cd.F1 = j;
cd.F2 = rnd.Next(i);
cd.F3 = "nima";
this.Add(cd);
}
return this;
}
}
和 XAML:
<Window x:Class="TestWPF.Page_ObjectDataProvider"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:TestWPF"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="ObjectDataProvider" Height="362" Width="360" Loaded="Window_Loaded">
<Window.Resources>
<ObjectDataProvider x:Key="ADUsers" ObjectType="{x:Type src:RetrievCustomData}"
MethodName="GetSome">
<ObjectDataProvider.MethodParameters>
<sys:Int32>20</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<ListView x:Name="lstUsers"
ItemsSource="{Binding Source={StaticResource ADUsers}}" Margin="0,0,0,106">
<ListView.View>
<GridView>
<GridViewColumn Header="User Name"
Width="80"
DisplayMemberBinding="{Binding Path=F1}" />
<GridViewColumn Header="Group Distinguished Name"
Width="80"
DisplayMemberBinding="{Binding Path=F3}" />
<GridViewColumn Header="Group Distinguished Name"
Width="80"
DisplayMemberBinding="{Binding Path=F2}" />
</GridView>
</ListView.View>
</ListView>
<Button Content="Get" Height="58" HorizontalAlignment="Left" Margin="64,253,0,0" Name="button1" VerticalAlignment="Top" Width="179" Click="button1_Click" />
</Grid>
如果我将 DataContext 或我的 ObjectDataProvider
设置为 null,那么它不会再次绑定。只是我想更新 ObjectDataProvider
并将新值绑定到我的 ListView
I bound a ListView to ObjectDataProvider.I get some value from user and change my ObjectDataProvider at runtime but when my ObjectDataProvider updated all of it's Item add to ListView and replace them.I use this statement:
lstUsers.Items.Clear();
but I get this error:
Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
How I can remove all data from listview when it's bind to ObjectDataProvider?
thanks
EDIT 1):
here is my code:
public partial class Page_ObjectDataProvider : Window
{
public Page_ObjectDataProvider()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
int myValue =10;
((ObjectDataProvider)this.FindResource("ADUsers")).MethodParameters.Clear();
((ObjectDataProvider)this.FindResource("ADUsers")).MethodParameters.Add(myValue);
((ObjectDataProvider)this.FindResource("ADUsers")).Refresh();
}
}
public class CustomData
{
public int F1 { get; set; }
public int F2 { get; set; }
public string F3 { get; set; }
}
public class RetrievCustomData : List<CustomData>
{
public RetrievCustomData GetSome(int i)
{
for (int j = 0; j < i; j++)
{
CustomData cd = new CustomData();
Random rnd = new Random();
cd.F1 = j;
cd.F2 = rnd.Next(i);
cd.F3 = "nima";
this.Add(cd);
}
return this;
}
}
and the XAML:
<Window x:Class="TestWPF.Page_ObjectDataProvider"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:TestWPF"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="ObjectDataProvider" Height="362" Width="360" Loaded="Window_Loaded">
<Window.Resources>
<ObjectDataProvider x:Key="ADUsers" ObjectType="{x:Type src:RetrievCustomData}"
MethodName="GetSome">
<ObjectDataProvider.MethodParameters>
<sys:Int32>20</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<Grid>
<ListView x:Name="lstUsers"
ItemsSource="{Binding Source={StaticResource ADUsers}}" Margin="0,0,0,106">
<ListView.View>
<GridView>
<GridViewColumn Header="User Name"
Width="80"
DisplayMemberBinding="{Binding Path=F1}" />
<GridViewColumn Header="Group Distinguished Name"
Width="80"
DisplayMemberBinding="{Binding Path=F3}" />
<GridViewColumn Header="Group Distinguished Name"
Width="80"
DisplayMemberBinding="{Binding Path=F2}" />
</GridView>
</ListView.View>
</ListView>
<Button Content="Get" Height="58" HorizontalAlignment="Left" Margin="64,253,0,0" Name="button1" VerticalAlignment="Top" Width="179" Click="button1_Click" />
</Grid>
if I set DataContext or my ObjectDataProvider
to null then It does not bind again.simply I want to update ObjectDataProvider
and bind new values to my ListView
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以清除 ListView 的 ItemsSource 属性来清除项目。
You can clear the ItemsSource property of the ListView to clear items.
WPF 没有 ObjectDataSource 类。您的意思是
ObjectDataProvider
吗?或者您只是意味着您正在使用对象集合作为数据源?ListView
(或任何项目控件)的数据源应该是实现INotifyCollectionChanged
的集合。 WPF 中最常用的类型是ObservableCollection
,但您还可以使用其他类型。如果您填充一个确实更改通知的集合,并将项目控件的
ItemsSource
绑定到该集合,则每当您从集合中添加或删除对象时,结果都会反映在项目控件的内容中显示。简而言之,要从
ListView
中删除所有项目,请清除ItemsSource
绑定到的集合。如果集合不支持更改通知,那么这将不起作用。在这种情况下,您必须刷新与项目源的绑定。例如,如果它绑定到支持更改通知的属性,您只需为该属性引发
PropertyChanged
,绑定就会刷新项目。如果您已在代码隐藏中设置了ItemsSource
,则可能需要将其设置为null
,然后再将其设置回来,这将强制刷新绑定。您可能会想,“这似乎是一个愚蠢的黑客”,您是对的:WPF 是围绕绑定和属性更改通知而设计的,如果您在代码隐藏中操作这些属性,那么您几乎就做错了。
WPF doesn't have an ObjectDataSource class. Do you mean
ObjectDataProvider
? Or do you just mean that you're using a collection of objects as your data source?The data source for a
ListView
(or any items control) should be a collection that implementsINotifyCollectionChanged
. The most commonly used type in WPF isObservableCollection<T>
, but there are other types that you could use.If you populate a collection that does change notification, and bind an items control's
ItemsSource
to the collection, then any time you add or remove an object from the collection, the result will be reflected in what the items control displays.In short, to remove all items from the
ListView
, clear the collection that theItemsSource
is bound to.If the collection doesn't support change notification, then this won't work. In this case, you have to refresh the binding to the items source. If it's bound to a property that supports change notification, for instance, you can just raise
PropertyChanged
for that property and the binding will refresh the items. If you've set theItemsSource
in code-behind, you'll probably have to set it tonull
and then set it back, which will force the binding to refresh.You may be thinking, "that seems like a stupid hack," and you're right: WPF is designed around binding and property-change notification, and if you manipulate these properties in code-behind you're pretty much doing it wrong.
您可以从 DataSource 中删除项目或将 ListView.DataSource 设置为 null。
You can either remove items from DataSource or set the ListView.DataSource to null.
简单的答案是明确数据的来源。然后将清除的源设置为列表视图的源。
这样你的列表视图仍然绑定到这个数据源。不要将源设置为空。
The simple answer is clear the source of your data. Then set the cleared source as the source to your List View.
This way your listview is still bound to this data source. Don't set the source to null.