0

我是 WPF 的新手,我的应用程序中有一个用户控件,它具有列表类型的依赖属性。

在我的窗口中,我放置了那个用户控件并将这个 DP 绑定到我的视图列表的属性。

最终属性会发生变化,因此作为集合但它不会反映在用户控件上。

我的视图实现了 INotifyPropertyChanged 接口。

我的用户控件的 DP 不会引发属性更改的事件。

可能的原因是什么?

我的用户控件中我的依赖属性的代码

  public List<ItemsResult> ItemsCollection
    {
        get { return (List<ItemsResult>)GetValue(ItemsCollectionProperty); }
        set { SetValue(ItemsCollectionProperty, value); }
    }

    public static readonly DependencyProperty ItemsCollectionProperty =
        DependencyProperty.Register("ItemsCollection", typeof(List<ItemsResult>), typeof(ListingUserControl)
                                    , new FrameworkPropertyMetadata(null, ItemsCollectionChanged));

    private static void ItemsCollectionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs eventArgs)
    {
        var listingUserControl = (obj as ListingUserControl);
        var itemsResult = (eventArgs.NewValue as List<ItemsResult>);
        if (listingUserControl != null && itemsResult != null)
            listingUserControl.CountLabelVisible = itemsResult.Count > 0;
    }

在我看来绑定

 <my:ListingUserControl ItemsCollection="{Binding Clients}" Title="Client" />
4

1 回答 1

0

在这种情况下,就像 HighCore 所写的那样,它不会在 collectionchange 的情况下触发任何事件,而只会在绑定更改时触发一次。您可以做的是,检查有界集合是否实现了 INotifyCollectionChanged。喜欢:

var itemsResult = (eventArgs.NewValue as INotifyCollectionChanged);
if (itemsResult != null)
{ 
   itemsResult.CollectionChanged += OnItemsSourceCollectionChanged;
}

然后您可以监视集合更改。

于 2013-10-30T10:14:20.990 回答