title: In wpf Clear ListBox items at Runtime
body: <p>I am using wpf <code>listbox,</code> 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(),  i tried lot ways, is there any problem in my XAML binding, the following is my code. </p>
  
  <p>XAML:</p>
  
  <pre><code> &lt;ListBox  ItemsSource="{Binding}"   HorizontalContentAlignment="Left" x:Name="lstbxindex"  Foreground="White" FontSize="20px" Height="400" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" &gt;
              &lt;ListBox.ItemTemplate&gt;    
                  &lt;DataTemplate&gt;
  
                      &lt;WrapPanel Orientation="Horizontal" Margin="5" &gt;
  
                          &lt;StackPanel Orientation="Horizontal"&gt;
                              &lt;TextBlock x:Name="txtblckroundhour" Height="40px" Width="55px" Text="{Binding RoundedHours}"  FontSize="14" Background="#555555" Loaded="txtblckroundhour_Loaded"  Foreground="White"&gt;&lt;/TextBlock&gt;
  
                              &lt;Label x:Name="items" MouseDoubleClick="items_MouseDoubleClick" Content="{Binding ProjectRow.Name}" Background="#555555" FontSize="20" Loaded="items_Loaded" Visibility="Visible"  Margin="35,0,0,0"  Width="230" Foreground="White"&gt;&lt;/Label&gt;
                          &lt;/StackPanel&gt;
  
  
                          &lt;StackPanel Orientation="Vertical"&gt;
                              &lt;ComboBox  Height="40px" Width="290" Margin="-230,0,0,0" Loaded="ComboBox_Loaded" Visibility="Hidden"  IsEditable="True"  FontSize="20"  Background="White"  Foreground="Black"&gt;&lt;/ComboBox&gt;
                          &lt;/StackPanel&gt;
  
                          &lt;!--&lt;ComboBox  x:Name="ComboBox_AddItem"  Height="40px" Width="290" Margin="-35,35,0,0"  Loaded="ComboBox_AddItem_Loaded"  IsEditable="True"  FontSize="20"  Background="White" Visibility="Hidden" Foreground="Black"&gt;&lt;/ComboBox&gt;--&gt;
                      &lt;/WrapPanel&gt;
                  &lt;/DataTemplate&gt;
  
  
  
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
  </code></pre>
  
  <p>Get the list of values</p>
  
  <pre><code>private List&lt;ProjectInformation&gt; projectInformationList1 = new List&lt;ProjectInformation&gt;();
  </code></pre>
  
  <p>// Here define the actual binding of the userinterface listbox to the in-memory list of objects.</p>
  
  <pre><code> foreach (DtoProjectsRow row in projectsTable.Rows)
                  {
  
                      projectInformationList1.Add(new ProjectInformation(row));
                  }
  
    lstbxindex.DataContext = projectInformationList1;
  </code></pre>
  
  <p>In SO I tried some solution but unfortunately it is not work for me. Last I tried,</p>
  
  <p>XAML.cs page</p>
  
  <pre><code>public static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(ObservableCollection&lt;String&gt;), typeof(Window));
  
          public ObservableCollection&lt;String&gt; MyList
          {
              get
              {
                  return (ObservableCollection&lt;String&gt;)GetValue(MyListProperty);
              }
              set
              {
                  SetValue(MyListProperty, value);
              }
          }
  </code></pre>
  
  <p>XAML:</p>
  
  <pre><code> &lt;ListBox  ItemsSource="{Binding **ElementName=Window**}"   HorizontalContentAlignment="Left" x:Name="lstbxindex"  Foreground="White" FontSize="20px" Height="400" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" &gt;
  </code></pre>
  
  <p>Using this above solution listitems are clear but when pageloading the listboxitems are clear but I don't want to clear the iistboxitems, after updating the values from user it will reload the updated value in listbox.</p>
  
  <p>lstbxindex.ItemsSource = null;</p>
  
  <p>But its not work.For pageload listbox loaded all items,every 15 min interval it will call the load function for firsttime it will reload the updatedvalues but second time it will reload the updated values and previous values remains in listbox again.</p>