Silverlight 列表框:绑定未更新

发布于 2024-11-19 01:09:09 字数 273 浏览 3 评论 0原文

我有一个包含多个步骤的向导控件。不可见的控件将从可视化树中删除。我在一个页面上有一个 ListBox,它绑定到 ObservableCollection。当在一个页面上的 ListBox 和另一页面上的 ListBox(具有相同的 ItemsSource)中添加或删除项目时,其他页面没有更新。我希望这足够清楚地解释我的问题。

当页面再次添加到可视化树时,如何更新此绑定?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北座城市 2024-11-26 01:09:09

我无法重现你的问题。我能够从可视化树中删除 ListBox,将对象添加到 ObservableCollection,当我将其添加到可视化树时,项目实际上会更新。

尝试通过设置“折叠”的可见性来解决您的问题,而不是从可视树中删除。

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文