Question

I am creating a custom control with a dependency property that I want to bind a custom class to. When the property gets update I want to call a method so that it can reconfigure the control... I am lost.

 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 
}

I bind to my property just fine:

<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"/>

The PropertyChangedCallback only gets called once and never calls the method again to update the control; however, the binding is getting updated and when the source gets updated the property gets updated...also onpropertychanged() does not get called in my viewmodel.

When I force onpropertychanged() to be called in my viewmodel by re-instantiating the object that is bound to my control - the binding works once but it does not update when the property changes even though onpropertychanged() is being called.

How am I supposed to know when the binding is updated? Is my dependencyproperty code correct?

Was it helpful?

Solution

There's a few things there that look wrong to me.

1 This isn't needed since the binding will do that for you. If you put a breakpoint here, you should see that dayView.Collection and e.NewValue are already the same.

dayView.Collection = e.NewValue as AppointmentCollections;

2 This also isn't needed. I think this is probably the main culprit since you're changing the DataContext to where "DayOneAppointmentCollections" no longer exists.

dayView.DataContext = dayView.Collection;

3 In SetupControl be sure to unsubscribe from the old collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top