Skip to main content
Home Forums Silverlight Programming Programming with .NET - General ObservableCollection - Clear has no Result in UI
6 replies. Latest Post by jroedig on May 27, 2009.
(0)
jroedig
Member
1 points
4 Posts
05-27-2009 9:31 AM |
Hello,
I have a ObservableCollection _userlist with User Data (Firstname, Lastename)...
This ObservableCollection is bound to an ListBox
If I click a Button, the _userlist.Clear Method clears the Collection. But in the User Interface the data are not cleared.
Can me anyone help?
1 // resultlist of userliste for databinding2 private ObservableCollection _userlist = new ObservableCollection();3 4 void usersearch_UserSearchLoadingComplete(object sender, Silverlight.Model.EventArguments.UserSearchLoadingEventArgs e)5 {6 // clear old results7 _userlist.Clear();8 9 // For each User in new Result collection add in _userlist10 foreach (User u in e.Results)11 {12 // add result user item13 _userlist.Add(u); 14 }15 16 // fire the event to notify UI17 UserSearchLoadingComplet(this, null);18 19 }20 21 22 23 24 public ObservableCollection UserResultlist25 { 26 get27 {28 return _userlist;29 }30 }
jay nana...
Contributor
3388 points
624 Posts
05-27-2009 9:56 AM |
Hi, try to do two way databinding for the ListBox.
05-27-2009 10:05 AM |
jay nanavati: Hi, try to do two way databinding for the ListBox.
Hey,
TwoWay Mode has no other result.
_userlist.Clear(); clears the collection. but in the listbox the items already existing. the Clear() Method clears not the listbox items...
EliteMike
65 points
25 Posts
05-27-2009 10:09 AM |
it almost seems like the binding is breaking somewhere. You aren't setting the itemsource to anything in code behind are you? One way binding is all you need for this as well.
05-27-2009 10:19 AM |
no, the itemsource is one time set in xaml code...
the binding works on line 13 _userlist.Add(...). If the add method is called, the useritem is shown in the listbox.
on line 7 the Clear has no result in the listbox
MarkTap
Participant
1442 points
263 Posts
05-27-2009 10:33 AM |
I was not able to reproduce your issue with a simplified example. Below is my code that seems to work as desired: when the button is clicked, the contents of the list box disappear. Which ObservableCollection are you using? The one I use is a generic which doesn't match your code.
---Page.xaml---
<ListBox Name="ListBoxUser" ItemsSource="{Binding}" > <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Firstname}"></TextBlock> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <Button Content="Button" Click="UserButton_Click"/>
----Page.xaml.cs----
public partial class Page : UserControl { ObservableCollection<UserData> _userData; public Page() { InitializeComponent(); _userData = new ObservableCollection<UserData>(); _userData.Add(new UserData() { Firstname = "f1", Lastname = "l1" }); _userData.Add(new UserData() { Firstname = "f2", Lastname = "l2" }); _userData.Add(new UserData() { Firstname = "f3", Lastname = "l3" }); DataContext = _userData; } private void UserButton_Click(object sender, RoutedEventArgs e) { _userData.Clear(); } } public class UserData { public string Firstname { get; set; } public string Lastname { get; set; } }
05-27-2009 11:04 AM |
thx, i have resolved the problem.