tags:

views:

124

answers:

5

Hai am using wpf list box, i cannot able to clear the list when am calling the reload data function, i just want to reload new data at runtime,while page loading it loads the data correctly, when i refresh the new data is fetched in itemsource i can see that in debug mode, but no new data in listbox, old data remains in the list, i cant even clear, when i call list.items.clear(), it throws exception and app crashes, i tried lot ways, is ther any problem in my XAML binding, the following is my code.

<DataTemplate x:Key="listBoxTemplate">
                <StackPanel Margin="3">
                    <DockPanel >
                        <TextBlock FontWeight="Bold" Text="{Binding Name}" DockPanel.Dock="Left" Margin="5,0,10,0"/>
                        <TextBlock Text="  " />
                         <TextBlock Text="{Binding Percnt}" Foreground="Green" FontWeight="Bold" />
                   </DockPanel>                       
                </StackPanel>
            </DataTemplate>

My listbox

 <ListBox Height="898" Name="lstEntity" Width="291" ItemTemplate="{StaticResource listBoxTemplate}" SelectionChanged="lstEntity_SelectionChanged"/>

Binding code

   lstEntity.ItemsSource = sei.getNames();

getNames() function just returns the data as list,nothing special code in that, How to resolve this.

A: 

Do you (ever) populate the ListBox by directly adding to the Items collection or just through ItemsSource?

If the latter, set the ItemsSource to null and set it back to reload.

Jeff M
if i set NULL also it throws the excetion as "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead." it also not working..????
deep
am not adding directly, just adding by itemsource only
deep
I'm out of ideas then sorry.
Jeff M
A: 

If you're using the MVVM pattern, add a property to your ViewModel:

public IEnumerable Names {
    get { return sei.getNames() as IEnumerable; }
}

Then, in your XAML, code ItemsSource as this:

<ListBox ... ItemsSource="{Binding Names}" ... />

Whenever the contents of your list of Names changes, raise the PropertyChanged event; this will tell the WPF system to refresh your ListBox:

PropertyChanged(this, new PropertyChangedEventArgs("Names");
Rob Perkins
am not using MVVM pattern. just normal binding
deep
Then, implement that property and the INotifyPropertyChanged interface directly on your class, if you can.
Rob Perkins
A: 

Can't say what's the cause of issue in your case until you give the exception and other details. However suggesting the better way of doing the thing.

  1. Let your getnames method return an IEnumerable.
  2. Construct an ObservableCollection out of it.
  3. Set the ItemsSource to the ObservableCollection created

Now you can alter your ObservableCollection to see the changes in the ListBox.

Veer
A: 

Before you do this :

  lstEntity.ItemsSource = sei.getNames();

Clear the listbox itemssource :

lstEntity.ItemsSource = "";
Malcolm
A: 

The best way to get this type of behavior is to use a DependencyProperty and bindings.

In your class file create the DP like so:

    #region MyList dependency property
    public static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(ObservableCollection<String>), typeof(Window1));

    public ObservableCollection<String> MyList
    {
        get { return (ObservableCollection<String>) GetValue(MyListProperty); }
        set { SetValue(MyListProperty, value); }
    }
    #endregion

Then in you XAML bind to that DP like so:

<ListBox ItemSource={Binding Path=MyList, ElementName=MyWindow} Height="898" Name="lstEntity" Width="291" ItemTemplate="{StaticResource listBoxTemplate}" SelectionChanged="lstEntity_SelectionChanged"/>

Where "MyWindow" is the x:Name of the root window in you XAML file (you could of course use datacontext like the MVVM pattern as well :)

Then if you want to add/remove items from your code you just access the list directly:

MyList.Clear();
MyList.Add("My New String");

You will, of course, also need to change the Generic type of the collection to be your own class...

Mark