2014-01-14 76 views
0

我正在创建一个自定义控件,其中包含要绑定自定义类的依赖项属性。当属性得到更新时,我想调用一个方法,以便它可以重新配置控件...我迷路了。如何知道何时绑定已更新依赖项属性

internal partial class CollectionScheduleDayView : Canvas 
    { 
     public static DependencyProperty AppointmentsCollectionProperty = DependencyProperty.Register("Collection", typeof(AppointmentCollections), typeof(CollectionScheduleDayView), new PropertyMetadata(null, PropertyChangedCallback)); 


     private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
     { 
      SetDataContext(dependencyObject, e); 
     } 

     public AppointmentCollections Collection 
     { 
      get 
      { 
       return GetValue(AppointmentsCollectionProperty) as AppointmentCollections; 
      } 
      set 
      { 
       SetValue(AppointmentsCollectionProperty, value); 
      } 
     } 

     public CollectionScheduleDayView() 
     { 
      InitializeComponent(); 
     } 

     void CollectionScheduleDayView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) 
     { 

     } 


     private static void SetDataContext(DependencyObject control, DependencyPropertyChangedEventArgs e) 
     { 
      var dayView = control as CollectionScheduleDayView; 

      if (e.NewValue != null) 
      { 
       if (dayView != null) 
       { 
        dayView.Collection = e.NewValue as AppointmentCollections; 
        dayView.DataContext = dayView.Collection; 
        dayView.SetupControl(); 
       } 
      } 
     } 

     public void SetupControl() 
     { 
      ResetControl(); 

      Collection.RescheduledAppointments.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(RescheduledAppointments_CollectionChanged); 
      Collection.CompletedAppointments.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(CompletedAppointments_CollectionChanged); 
      Collection.RemainingAppointments.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(RemainingAppointments_CollectionChanged); 

      if (Collection.RescheduledAppointments == null || Collection.RescheduledAppointments.Count == 0) 
      { 
       CollapseRescheduled(); 
      } 
      if (Collection.CompletedAppointments == null ||Collection.CompletedAppointments.Count == 0) 
      { 
       CollapseCompleted(); 
      } 
     } 

     //...some other code below 
} 

我绑定到我的财产就好:

<my:CollectionScheduleDayView Grid.Column="0" HorizontalAlignment="Left" Margin="12,35,0,0" x:Name="DayView0" VerticalAlignment="Top" Collection="{Binding DayOneAppointmentCollections, NotifyOnSourceUpdated=True}" SourceUpdated="CollectionScheduleDayView1_OnSourceUpdated"/> 

PropertyChangedCallback只被调用一次,不会再调用该方法以更新控制;然而,绑定正在更新,当源得到更新时,属性得到更新... onpropertychanged()不会在我的viewmodel中被调用。

当我强制onpropertychanged()在我的viewmodel中通过重新实例化绑定到我的控件的对象时调用 - 绑定工作一次,但它不更新时属性更改即使onpropertychanged()被调用。

我该如何知道绑定更新的时间?我的依赖属性代码是否正确?

+0

请注意,您的PropertyChangedCallback用于AppointmentsCollectionProperty,它保存对集合的引用。当然,改变这个属性意味着改变引用,为其分配另一个集合对象。如果你没有为属性指定另一个集合对象(或null),那么这个属性显然不会改变......如果你想知道集合本身内是否有东西发生了变化,那么你将不得不订阅相应的该集合的事件(ObservableCollection提供这样的事件)。 – elgonzo

+0

在上面的代码我这样做:'Collection.RescheduledAppointments.CollectionChanged + = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(RescheduledAppointments_CollectionChanged);'其中'Collection.RescheduledAppointments'是一个ObservableCollection - 这不工作... – jharr100

+0

好吧,然后对你的问题场景更具体:如果你添加/删除项目到你添加了CollectionChanged处理程序的集合对象,CollectionChanged是不会被触发的?或者是你的问题,而不是你知道何时(例如)Collection.RescheduledAppointments设置为另一个ObservableCollection?从你的问题来看,你不清楚你的问题究竟在哪里/哪里。 – elgonzo

回答

0

有一些东西看起来不对我。

1这是不需要的,因为绑定会为你做。如果你在这里放置一个断点,你应该看到dayView.Collection和e.NewValue已经是相同的了。

dayView.Collection = e.NewValue as AppointmentCollections; 

2这也是没有必要的。我认为这可能是您将DataContext更改为“DayOneAppointmentCollections”不再存在的主要罪魁祸首。

dayView.DataContext = dayView.Collection; 

3在SetupControl中,一定要取消订阅旧集合。

+0

上调用onprorpertychanged,如果我不设置datacontext,那么datacontext设置为我的viewmodel ... – jharr100

+0

基于接受,而不是设置DataContext为你工作? – TTat

+0

这是所有三个组合 - 我认为它更加如此去除事件。不设置数据上下文似乎很容易,以避免考虑你可以使用relativesource绑定到控件 - 我仍然试图实现 - 但这将是一个不同的问题...你和@elgonzo都给了好的建议 – jharr100