不调用DependencyProperty getter / setter [英] DependencyProperty getter/setter not being called

查看:140
本文介绍了不调用DependencyProperty getter / setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个从标准网格派生的自定义控件。
我添加了一个ObservableCollection作为自定义控件的DependencyProperty。但是,它的获取/设置从未达到。

 公共类MyGrid:网格
{0}我可以在创建可正确使用和ObservableCollection的DependencyProperty方面有一些指导原则吗?
public ObservableCollection< string> Items
{
get
{
return(ObservableCollection< string>)GetValue(ItemsProperty);
}
set
{
SetValue(ItemsProperty,value);



public static DependencyProperty ItemsProperty =
DependencyProperty.Register(Items,typeof(ObservableCollection< string>),
typeof(MyGrid ),新的UIPropertyMetadata(null,OnItemsChanged));



解决方案

建议不要使用ObservableCollection作为 Items 依赖项属性的类型。



这里有一个ObservableCollection我猜)是当赋值属性值时,使UserControl附加一个 CollectionChanged 处理程序。但ObservableCollection太具体。



WPF中的方法(例如 IEnumerable )。当属性被分配一个值时,找出值集合是否实现了某些更具体的接口。这里至少要 INotifyCollectionChanged ,但该集合也可能实现 ICollectionView INotifyPropertyChanged 。所有这些接口都不是必需的,这将使您的依赖属性能够绑定到各种集合,从一个普通的数组开始,直到一个复杂的 ItemCollection



您的 OnItemsChanged property change callback会看起来像这样:

pre $ private $ static $ OnItemsChanged(DependencyObject obj,DependencyPropertyChangedEventArgs e)
{
MyGrid grid = obj as MyGrid;

if(grid!= null)
{
var oldCollectionChanged = e.OldValue as INotifyCollectionChanged;
var newCollectionChanged = e.NewValue作为INotifyCollectionChanged;

if(oldCollectionChanged!= null)
{
oldCollectionChanged.CollectionChanged - = grid.OnItemsCollectionChanged;
}

if(newCollectionChanged!= null)
{
newCollectionChanged.CollectionChanged + = grid.OnItemsCollectionChanged;

//除了添加一个CollectionChanged处理函数
//任何已经存在的集合元素都应该在这里处理




private void OnItemsCollectionChanged(object sender,NotifyCollectionChangedEventArgs e)
{
//在这里处理集合更改
}


I am trying to create a Custom control derived from a standard Grid. I added a ObservableCollection as a DependencyProperty of the Custom control. However, the get/set of it is never reached. Can I have some guidelines in creating a DependencyProperty that works correctly with and ObservableCollection?

public class MyGrid : Grid
{
    public ObservableCollection<string> Items
    {
        get
        {
            return (ObservableCollection<string>)GetValue(ItemsProperty);
        }
        set
        {
            SetValue(ItemsProperty, value);
        }
    }

public static  DependencyProperty ItemsProperty =
                DependencyProperty.Register("Items", typeof(ObservableCollection<string>), 
        typeof(MyGrid), new UIPropertyMetadata(null, OnItemsChanged));

}

解决方案

I would suggest not to use ObservableCollection as the type of an Items dependency property.

The reason for having an ObservableCollection here (I guess) is to enable the UserControl to attach a CollectionChanged handler when the property value is assigned. But ObservableCollection is too specific.

The approach in WPF (e.g. in ItemsControl.ItemsSource) is to define a very basic interface type (like IEnumerable) and when the property is assigned a value, find out if the value collection implements certain more specific interfaces. This would at least be INotifyCollectionChanged here, but the collection might also implement ICollectionView and INotifyPropertyChanged. All these interfaces wouldn't be mandatory and that would enable your dependency property to bind to all sorts of collections, starting with a plain array up to a complex ItemCollection.

Your OnItemsChanged property change callback would then look like this:

private static void OnItemsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    MyGrid grid = obj as MyGrid;

    if (grid != null)
    {
        var oldCollectionChanged = e.OldValue as INotifyCollectionChanged;
        var newCollectionChanged = e.NewValue as INotifyCollectionChanged;

        if (oldCollectionChanged != null)
        {
            oldCollectionChanged.CollectionChanged -= grid.OnItemsCollectionChanged;
        }

        if (newCollectionChanged != null)
        {
            newCollectionChanged.CollectionChanged += grid.OnItemsCollectionChanged;

            // in addition to adding a CollectionChanged handler
            // any already existing collection elements should be processed here
        }
    }
}

private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    // handle collection changes here
}

这篇关于不调用DependencyProperty getter / setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆