创建项目DP以绘制用户控件图表

时间:2015-01-27 16:25:55

标签: c# wpf binding observablecollection dependency-properties

我正在忙于创建一个具有一些基本图表/图形功能的用户控件。本质上,我想要一个“Items”依赖属性,控件的用户可以绑定到该属性。然后,控件将显示对源进行的所有项目和更新。

到目前为止,我所做的是在我的用户控件中创建一个“Items”DP,后面是代码。

public static readonly DependencyProperty ItemsProperty =
    DependencyProperty.Register("Items",
    typeof(ObservableCollection<Polyline>),
    typeof(RPGraph),
    new FrameworkPropertyMetadata(
    new ObservableCollection<Polyline>(),
    new PropertyChangedCallback(OnItemsChanged)));

public ObservableCollection<Polyline> Items 
{
    get { return (ObservableCollection<Polyline>)GetValue(ItemsProperty); }
    set { SetValue(ItemsProperty, value);  }
}

public static void OnItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}

我的第一个绊脚石是当我的收藏改变时,“OnItemsChanged”没有被调用。几个小时后,我发现了一个stackoverflow帖子,解释了为什么(ObservableCollection dependency property does not update when item in collection is deleted)。遵循这个建议解决了我的问题的一部分。现在我可以将新项目(折线)添加到ObservableCollection列表中。但是,如果我在折线中添加了一个额外的点或修改了一个点,该怎么办呢?有了上一个问题的知识,我找到了Points.Changed事件。然后我订阅了它并将更新代码放在那里。

这最终有效,但是男人必须有更好或更优雅的方式来实现这一点(如上所述),我认为所有归结为不使用ObservableCollection?有什么建议吗?

下面是工作OnItemChanged方法(原因是草稿代码,我只是想让它工作:-):

    public static void OnItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {     
        var thisControl = d as RPGraph;

        foreach (Polyline poly in thisControl.Items)
            thisControl.manager.Items.Add(poly.Points.ToArray());

        if (e.OldValue != null)
        {
            var coll = (INotifyCollectionChanged)e.OldValue;

            // Unsubscribe from CollectionChanged on the old collection
            coll.CollectionChanged -= Items_CollectionChanged;
        }

        if (e.NewValue != null)
        {
            var coll = (ObservableCollection<Polyline>)e.NewValue;

            // Subscribe to CollectionChanged on the new collection
            coll.CollectionChanged += (o, t) => {
                ObservableCollection<Polyline> items = o as ObservableCollection<Polyline>;

                thisControl.manager.Items.Add(items[t.NewStartingIndex].Points.ToArray());

                foreach (Polyline poly in items)
                {
                    poly.Points.Changed += (n, m) => {

                        for (int i = 0; i < thisControl.manager.Items.Count; i++)
                            thisControl.manager.Items[i] = thisControl.Items[i].Points.ToArray();

                        thisControl.manager.DrawGraph(thisControl.graphView);
                    };
                }
                thisControl.manager.DrawGraph(thisControl.graphView); 
            };
        }
        thisControl.manager.DrawGraph(thisControl.graphView);  
    }

1 个答案:

答案 0 :(得分:0)

您完全正确,ObservableCollection在其任何项目更改其属性值时不会通知。

您可以扩展ObservableCollection为这些案例添加通知的功能。

看起来像这样:

public sealed class ObservableNotifiableCollection<T> : ObservableCollection<T> where T : INotifyPropertyChanged
{
    public event ItemPropertyChangedEventHandler ItemPropertyChanged;
    public event EventHandler CollectionCleared;

    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
    {
        base.OnCollectionChanged(args);

        if (args.NewItems != null)
        {
            foreach (INotifyPropertyChanged item in args.NewItems)
            {
                item.PropertyChanged += this.OnItemPropertyChanged;
            }
        }

        if (args.OldItems != null)
        {
            foreach (INotifyPropertyChanged item in args.OldItems)
            {
                item.PropertyChanged -= this.OnItemPropertyChanged;
            }
        }
    }

    protected override void ClearItems()
    {
        foreach (INotifyPropertyChanged item in this.Items)
        {
            item.PropertyChanged -= this.OnItemPropertyChanged;
        }

        base.ClearItems();
        this.OnCollectionCleared();
    }

    private void OnCollectionCleared()
    {
        EventHandler eventHandler = this.CollectionCleared;
        if (eventHandler != null)
        {
            eventHandler(this, EventArgs.Empty);
        }
    }

    private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs args)
    {
        ItemPropertyChangedEventHandler eventHandler = this.ItemPropertyChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new ItemPropertyChangedEventArgs(sender, args.PropertyName));
        }
    }
}

然后你可以订阅ItemPropertyChanged事件并做你的事情。