如何处理添加到ObservableCollection类型的附加属性的项目

时间:2018-02-15 18:39:34

标签: wpf uwp

我有一个使用MapControl的UWP项目,这是一个密封的类 - 无法从中派生出一个新类。 试图创建一个可绑定的附加属性,它可以访问MapControl.Children。 问题是它只在我设置ViewModel的集合时有效,但在我向该集合添加新元素时却没有:

// Works fine
this.MapChildrenExtCollection = new ObservableCollection<MapChildElement>();

// Nothing happens
this.MapChildrenExtCollection.Add(new MapChildElement());

继承我附加财产的代码:

namespace UWPMap.Extensions
{
    public class MapControlExt : DependencyObject
    {
        public static readonly DependencyProperty ChildrenExtProperty = DependencyProperty.Register(
            "ChildrenExt",
            typeof(ObservableCollection<MapChildElement>),
            typeof(MapControlExt),
            new PropertyMetadata(new ObservableCollection<MapChildElement>(), ChildrenExtPropertyChanged));

        public ObservableCollection<MapChildElement> ChildrenExt
        {
            get { return (ObservableCollection<MapChildElement>)GetValue(ChildrenExtProperty); }
            set { SetValue(ChildrenExtProperty, value); }
        }

        public static void SetChildrenExt(UIElement element, ObservableCollection<MapChildElement> value)
        {
            element.SetValue(ChildrenExtProperty, value);
        }

        public static ObservableCollection<MapChildElement> GetChildrenExt(UIElement element)
        {
            return (ObservableCollection<MapChildElement>)element.GetValue(ChildrenExtProperty);
        }

        private static void ChildrenExtPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var control = (MapControl)obj;
            var oldCollection = e.OldValue as INotifyCollectionChanged;
            var newCollection = e.NewValue as INotifyCollectionChanged;

            if (oldCollection != null)
            {
                oldCollection.CollectionChanged -= Extensions.MapControlExt.ChildrenExtCollectionChanged;
            }

            if (newCollection != null)
            {
                oldCollection.CollectionChanged += Extensions.MapControlExt.ChildrenExtCollectionChanged;
            }

            ManageChildrenExt();
        }

        static void ChildrenExtCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            ManageChildrenExt();
        }

        static private void ManageChildrenExt()
        {
            // Access MapControl.Children here
        }
    }
}

XAML:

<maps:MapControl x:Name="MyMap"
                 ext:MapControlExt.ChildrenExt="{x:Bind Path=MapChildrenExtCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</maps:MapControl>

1 个答案:

答案 0 :(得分:1)

问题是您没有将事件处理程序添加到新集合中并错误地使用oldCollection变量。

以下代码段:

if (newCollection != null)
{
    oldCollection.CollectionChanged += //here you have oldCollection by mistake
       Extensions.MapControlExt.ChildrenExtCollectionChanged;
}

应该是:

if (newCollection != null)
{
    newCollection.CollectionChanged += 
       Extensions.MapControlExt.ChildrenExtCollectionChanged;
}