【问题标题】:WPF - UI Not Updating When Item Added to Bound CollectionWPF - 将项目添加到绑定集合时 UI 不更新
【发布时间】:2011-05-25 15:38:47
【问题描述】:

我有一个绑定到自定义控件的对象的通用列表。在代码隐藏中一切似乎都可以正常工作,但我对集合所做的任何更改似乎都没有反映在 UI 中(即使它们在所有隐藏的代码中都可以找到)。

这是我的 UI 的 XAML:

<controls:ControllableListView x:Name="lvSummaryCaseEvents" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Label="Case Events:" ItemsSource="{Binding CaseEvents}" AddButtonClicked="ControllableListView_AddButtonClicked">

以及我将项目添加到集合中的代码:

_caseScreen.CaseEvents.Add(caseEvent);
//after the line above executes, lvSummaryCaseEvents (in the debugger) shows the correct number of items and the items' values are all correct.  No change to the UI whatsoever

我的用户控件中的 ItemSource 属性:

public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(System.Collections.IList), typeof(ControllableListView));

public System.Collections.IList ItemsSource
{
    get
    {
        return this.GetValue(ItemsSourceProperty) as System.Collections.IList;
    }
    set
    {
        this.SetValue(ItemsSourceProperty, value);
    }
}

最后,我的用户控件中的标准 ListView 的 XAML 绑定到上面列出的 ItemsSource 属性:

<ListView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" Name="lvListView" ItemsSource="{Binding ItemsSource}" View="{Binding View}" SelectionChanged="lvListView_SelectionChanged" />

重申一下,当我第一次显示集合时一切正常,但是当我向集合中添加项目时,UI 不会反映所做的更改。

提前致谢,
桑尼

【问题讨论】:

  • _caseScreen.CaseEvents 是什么类型?

标签: wpf user-interface binding


【解决方案1】:

将您的收藏更改为ObservableCollection&lt;T&gt;

【讨论】:

    猜你喜欢
    • 2011-03-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2011-01-10
    相关资源
    最近更新 更多