DependencyProperty对象未更新到UI

时间:2015-01-05 14:15:38

标签: c# wpf observablecollection dependency-properties inotifypropertychanged

我正在努力控制中的dependencyproperty。我的dependencyproperty是一个如下所示的对象:

public class ChartGroupCollection : ObservableCollection<ChartGroup>, INotifyCollectionChanged
{
    public void ClearDirty()
    {
        foreach (var grp in base.Items)
        {
            foreach(var run in grp.ChartRuns.Where(x=>x.IsDirty))
            {
                run.IsDirty = false;
            }

            grp.IsDirty = false;
        }
    }

    [XmlIgnore]
    public bool IsDirty  //dirty flag for save prompt
    {
        get
        {
            ....
        }
    }

} 

[Serializable]
public class ChartGroup : INotifyPropertyChanged
{ ... //various properties }

DependencyProperty设置如此(名为Tree,它是ChartGroupCollection的实例):

public static readonly DependencyProperty TreeProperty = DependencyProperty.Register("Tree", typeof(ChartGroupCollection), typeof(ChartsControl), new PropertyMetadata(OnTreeChanged));
    public ChartGroupCollection Tree
    {
        get { return (ChartGroupCollection)GetValue(TreeProperty); }
        set { SetValue(TreeProperty, value); }
    }
 private static void OnTreeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var Treee = sender as ChartsControl;
        if (e.OldValue != null)
        {
            var coll = (INotifyCollectionChanged)e.OldValue;
            coll.CollectionChanged -= Tree_CollectionChanged;
        }
        if (e.NewValue != null)
        {
            var coll = (ObservableCollection<ChartGroup>)e.NewValue;
            coll.CollectionChanged += Tree_CollectionChanged;
        }

    }

    private static void Tree_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        (sender as ChartsControl).OnTreeChanged();
    }

    void OnTreeChanged()
    {
        MessageBox.Show("Do something...");  //RefreshCharts();
    }

我似乎只是在创建对象时才进入OnTreeChanged事件,但是一旦我做了其他工作(添加到ChartGroup内的列表或更改ChartGroup对象的属性,甚至删除了observablecollection的元素,它似乎永远不会触发刷新事件。我已经尝试了其他几种方法来获取在线发现的依赖项属性,但没有解决方案对我有用。我想知道是否归结于我的dependencyproperty对象的内在性质或来自我身边的错误

1 个答案:

答案 0 :(得分:0)

sender处理程序中的Tree_CollectionChanged参数不是ChartsControl实例,而是引发CollectionChanged事件的集合。

Tree_CollectionChanged方法不应该是静态的

private void Tree_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    OnTreeChanged();
}

它应该像这样附加和删除:

private static void OnTreeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    var control = sender as ChartsControl;
    var oldCollection = e.OldValue as INotifyCollectionChanged;
    var newCollection = e.NewValue as INotifyCollectionChanged;

    if (oldCollection != null)
    {
        oldCollection.CollectionChanged -= control.Tree_CollectionChanged;
    }

    if (newCollection != null)
    {
        newCollection.CollectionChanged += control.Tree_CollectionChanged;
    }
}

另请注意,添加CollectionChanged处理程序并不关心订阅集合元素的PropertyChanged事件。每当向集合中添加元素或从集合中删除元素时,您还必须附加或删除PropertyChanged处理程序。看一下NotifyCollectionChangedEventArgs.Action属性。