How to Remove all Item in ListView when it bindto ObjectDataProvider

Arian picture Arian · Jul 30, 2011 · Viewed 8.3k times · Source

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

Answer

Yiğit Yener picture Yiğit Yener · Jul 30, 2011

You can clear the ItemsSource property of the ListView to clear items.

lstUsers.ClearValue(ListView.ItemsSourceProperty);