Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As Tormod suggested, the preferable methods would be changing the collection to an ObservableCollection, or implementing INotifyCollectionChanged in the collection would take care of refreshing the UI. </p> <p>However, if those options aren't available, then you can 'force' a refresh by using INotifyPropertyChanged in whatever class contains the collection. We then will be treating the list just like a regular property, and using the setter to notify on changes. To do this it requires re-assigning the reference, which is why using something like an ObservableCollection is preferred, as well as raising the PropertyChanged event.</p> <p>Here is a quick sample showing how this can be done with just a standard generic List:</p> <pre><code>public partial class Window1 : Window, INotifyPropertyChanged { public Window1() { InitializeComponent(); this.Names = new List&lt;string&gt;() { "Mike", "Robert" }; this.DataContext = this; } private IList&lt;string&gt; myNames; public IList&lt;string&gt; Names { get { return this.myNames; } set { this.myNames = value; this.NotifyPropertyChanged("Names"); } } private void OnAddName(object sender, RoutedEventArgs e) { Names.Add("Kevin"); Names = Names.ToList(); } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } </code></pre> <p>Xaml:</p> <pre><code>&lt;Window x:Class="Sample.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"&gt; &lt;Grid&gt; &lt;StackPanel&gt; &lt;ListBox ItemsSource="{Binding Names}" /&gt; &lt;Button Content="Add Name" Click="OnAddName" /&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload