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