How to Remove all Item in ListView when it bindto ObjectDataProvider
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
You can clear the ItemsSource property of the ListView to clear items.
lstUsers.ClearValue(ListView.ItemsSourceProperty);
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 implements INotifyCollectionChanged
. The most commonly used type in WPF is ObservableCollection<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 the ItemsSource
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 the ItemsSource
in code-behind, you'll probably have to set it to null
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.
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 is where you get my items source
List.Clear();
//Set the clear list at the items source again.
ListView.ItemsSource = List;
This way your listview is still bound to this data source. Don't set the source to null.
精彩评论