Bas
Bas

Reputation: 27085

Silverlight Listbox: Binding does not get updated

I have a Wizard control that has multiple steps. Controls that are not visible get removed from the visual tree. I have a ListBox on one page, that binds to an ObservableCollection<T>. When items get added or removed to that ListBoxon one page, the ListBox on another page (with the same ItemsSource), the binding on the other page does not get updated. I hope this explains my problem clearly enough.

How do I get this binding to update when the page gets added to the visual tree again ?

Upvotes: 3

Views: 960

Answers (1)

Chui Tey
Chui Tey

Reputation: 5554

I cannot reproduce your problem. I was able to remove a ListBox from the visual tree, add objects to the ObservableCollection, and when I add it to the visual tree, items are actually updated.

Try working around your problem by setting visibility to Collapsed rather than removing from Visual Tree.

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        this.listBox1.ItemsSource = o;
        this.listBox2.ItemsSource = o;
    }

    ObservableCollection<int> o = new ObservableCollection<int>();
    private void buttonList1_Click(object sender, RoutedEventArgs e)
    {
        if (this.listBox1.Parent == null)
            this.LayoutRoot.Children.Add(this.listBox1);
        else
            this.LayoutRoot.Children.Remove(this.listBox1);

        //this.listBox1.Visibility = this.listBox1.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
    }

    private void buttonList2_Click(object sender, RoutedEventArgs e)
    {
        if (this.listBox2.Parent == null)
            this.LayoutRoot.Children.Add(this.listBox2);
        else
            this.LayoutRoot.Children.Remove(this.listBox2);

        //this.listBox2.Visibility = this.listBox2.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
    }

    private void ButtonAddMore_Click(object sender, RoutedEventArgs e)
    {
        o.Add(o.Count);
    }

}

Upvotes: 1

Related Questions