Skip to main content

Microsoft Silverlight

Answered Question ObservableCollection - Clear has no Result in UIRSS Feed

(0)

jroedig
jroedig

Member

Member

1 points

4 Posts

ObservableCollection - Clear has no Result in UI

 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 databinding
2 private ObservableCollection _userlist = new ObservableCollection();
3
4 void usersearch_UserSearchLoadingComplete(object sender, Silverlight.Model.EventArguments.UserSearchLoadingEventArgs e)
5 {
6 // clear old results
7 _userlist.Clear();
8
9 // For each User in new Result collection add in _userlist
10 foreach (User u in e.Results)
11 {
12 // add result user item
13 _userlist.Add(u);
14 }
15
16 // fire the event to notify UI
17 UserSearchLoadingComplet(this, null);
18
19 }
20
21
22
23
24 public ObservableCollection UserResultlist
25 {
26 get
27 {
28 return _userlist;
29 }
30 }
 
  <UserControl.Resources>
        <vm:ViewModel x:Key="ViewModel"></vm:ViewModel>        
    </UserControl.Resources>
    
    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModel}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="0.106*"/>
            <RowDefinition Height="0.894*"/>
        </Grid.RowDefinitions>

        <ListBox Margin="15,15,15,15" Name="ListBoxUser" ItemsSource="{Binding Path=UserResultlist}" Grid.RowSpan="1" Grid.Row="1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Firstname}"></TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
            
        </ListBox>
        <Button HorizontalAlignment="Center" Margin="0,0,0,0" Content="Button" x:Name="UserButton" Click="UserButton_Click"/>      

jay nanavati
jay nana...

Contributor

Contributor

3388 points

624 Posts

Re: ObservableCollection - Clear has no Result in UI

 Hi, try to do two way databinding for the ListBox.

Jay K Nanavaty
www.technologyopinion.com
Mark as answer if it helps. It will also help others...

jroedig
jroedig

Member

Member

1 points

4 Posts

Re: ObservableCollection - Clear has no Result in UI

 

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
EliteMike

Member

Member

65 points

25 Posts

Re: ObservableCollection - Clear has no Result in UI

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.

jroedig
jroedig

Member

Member

1 points

4 Posts

Re: ObservableCollection - Clear has no Result in UI

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
MarkTap

Participant

Participant

1442 points

263 Posts

Answered Question

Re: ObservableCollection - Clear has no Result in UI

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; }
    }

jroedig
jroedig

Member

Member

1 points

4 Posts

Re: ObservableCollection - Clear has no Result in UI

 thx, i have resolved the problem.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities