ObservableCollection在Silverlight中具有Dependency属性

时间:2011-10-18 07:21:29

标签: silverlight

最近我在Silverlight中开发了一个自定义控件,我创建了一个类型为ObservableCollection的自定义依赖项属性。我有另外2个类型字符串的自定义依赖项属性。我的要求是添加任何项目到集合,我必须触发collectionChanged事件,在这个事件处理程序中,我想更新其他2个依赖项属性。

public static readonly DependencyProperty itemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<ValidationErrorMessage>), typeof(SummaryUserControl), new PropertyMetadata(new ObservableCollection<ValidationErrorMessage>(), new PropertyChangedCallback(fun1)));
    public ObservableCollection<ValidationErrorMessage> Items
    {
        get
        {
            return (ObservableCollection<ValidationErrorMessage>)base.GetValue(itemsProperty);
        }
        set
        {

            base.SetValue(itemsProperty, value);
        }
    }


public static void fun1(object sender, DependencyPropertyChangedEventArgs       evt)
    {
        var newValue =   evt.NewValue as ObservableCollection<ValidationErrorMessage>;

        if(newValue!=null)
            newValue.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChangedHandler);

        var oldValue  = evt.OldValue as ObservableCollection<ValidationErrorMessage>;
        if(oldValue!=null)
            oldValue.CollectionChanged -= new NotifyCollectionChangedEventHandler(CollectionChangedHandler);
    }

    static void CollectionChangedHandler(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
           var newItems =     e.NewItems as ObservableCollection<ValidationErrorMessage>;
           foreach (var item in newItems)
            {
                item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    static void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {

    }



public static readonly DependencyProperty headerProperty = DependencyProperty.Register("Header", typeof(String), typeof(SummaryUserControl), new PropertyMetadata(String.Empty, null));
    public String Header
    {
        get
        {
            return (String)base.GetValue(headerProperty);
        }
        set
        {
            base.SetValue(headerProperty, value);
            RaisePropertyChange("Header");
        }
    }

    public static readonly DependencyProperty messageTypeProperty =
    DependencyProperty.Register("MessageType", typeof(MessageEnumType), typeof(SummaryUserControl), new PropertyMetadata(MessageEnumType.Error, null));
    public MessageEnumType MessageType
    {
        get { return (MessageEnumType)GetValue(messageTypeProperty); }
        set { SetValue(messageTypeProperty, value); RaisePropertyChange("MessageType"); }
    }

如何更改依赖项属性messageType和Header的值?我无法在CollectionChangedNotifyPropertyChanged事件中访问这些属性,因为所有这些事件都是静态的。我无法访问这些静态事件处理程序中的实例。

我尝试使用转换器解决问题,但我对Silverlight的好奇使我想要使用上述方法。如何在CollectionChanged事件或NotifyPropertyChanged事件中设置这些依赖项属性的值?

1 个答案:

答案 0 :(得分:1)

静态 fun1 方法中的 sender 应该是声明itemsProperty DependencyProperty的类的实例。因此,您可以通过将发件人强制转换为您的类来访问具体实例。

public static void fun1(object sender, DependencyPropertyChangedEventArgs       evt) 
{
     MyClass concreteInstance = sender as MyClass;
     if(concreateInstance != null)
     {
         [...your code...]
     }
}