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 r开发者_Go百科emains 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.
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...
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.
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.
- Let your
getnames
method return an IEnumerable. - Construct an ObservableCollection out of it.
- Set the ItemsSource to the ObservableCollection created
Now you can alter your ObservableCollection to see the changes in the ListBox.
Before you do this :
lstEntity.ItemsSource = sei.getNames();
Clear the listbox itemssource :
lstEntity.ItemsSource = "";
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");
精彩评论