我正在創建一個自定義控件,其中包含要綁定自定義類的依賴項屬性。當屬性得到更新時,我想調用一個方法,以便它可以重新配置控件...我迷路了。如何知道何時綁定已更新依賴項屬性
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()被調用。
我該如何知道綁定更新的時間?我的依賴屬性代碼是否正確?
請注意,您的PropertyChangedCallback用於AppointmentsCollectionProperty,它保存對集合的引用。當然,改變這個屬性意味着改變引用,爲其分配另一個集合對象。如果你沒有爲屬性指定另一個集合對象(或null),那麼這個屬性顯然不會改變......如果你想知道集合本身內是否有東西發生了變化,那麼你將不得不訂閱相應的該集合的事件(ObservableCollection提供這樣的事件)。 – elgonzo
在上面的代碼我這樣做:'Collection.RescheduledAppointments.CollectionChanged + = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(RescheduledAppointments_CollectionChanged);'其中'Collection.RescheduledAppointments'是一個ObservableCollection - 這不工作... – jharr100
好吧,然後對你的問題場景更具體:如果你添加/刪除項目到你添加了CollectionChanged處理程序的集合對象,CollectionChanged是不會被觸發的?或者是你的問題,而不是你知道何時(例如)Collection.RescheduledAppointments設置爲另一個ObservableCollection?從你的問題來看,你不清楚你的問題究竟在哪裏/哪裏。 – elgonzo