tags:

views:

86

answers:

1

I'm writing a simple calendar control for better understanding of WPF. I have a CalendarHeader control that contains two buttons (next,prevoious) and a dependency property CurrentMonth defined as

 public static DependencyProperty CurrentMonthProperty = DependencyProperty.Register
        ("CurrentMonth", typeof(int), typeof(CalendarHeader), new FrameworkPropertyMetadata(DateTime.Now.Month, FrameworkPropertyMetadataOptions.None, CurrentMonthChangedCallBack));

Another control that need so interact with CalenderHeader is CalendarMonth. To inform CalendarMonth when a month is changed in CalendarHeader, I'm adding an owner dependency property to it as

public static DependencyProperty CurrentMonthProperty = CalendarHeader.CurrentMonthProperty.AddOwner
        (typeof(CalendarMonth), new FrameworkPropertyMetadata(DateTime.Now.Month, CurrentMonthChangedCallBack));

When CalendarHeader changes the value of CurrentMonth, I thought it would also trigger the CurrentMonthChangedCallBack in CalendarMonth but it's not doing that. How can I inform CalendarMonth that the value of CurrentMonth in CalendarHeader is changed. Thanks a million

A: 

One option is to create a base control (let's call it CalendarPrimitive) to store the CurrentMonthProperty. When you register the property register with FrameworkPropertyMetadataOptions.Inherit.

If you derive CalendarHeaderControl and CalendarMonth control from the CalendarPrimitive, the value will automatically pass down the tree from CalendarHeader to CalendarMonth (assuming that CalendarMonth is nested within CalendarHeader).

Even better if your top level Calendar control also derives from CalendarPrimitive, It will be responsible for storing the CurrentMonthProperty and all of its Children will inherit the value.

Also, you probably want to define a CurrentMonthChanged RoutedEvent that a CalendarPrimitive can use to notify it's the top level that it has changed the CurrentMonth.

Your top level Calendar will listen for this Event and change its CurrentMonth accordingly (propagating the change down the tree).

Mike Brown
Mike, I'm not sure if that's gonna work. First, CalendarHeader is nested inside CalendarMonth. Secondly, it's CalendarHeader that has buttons that change the month. So inheriting from base class insn't going to do much as it's not the base class that's changing the value but it's CalendarHeader.
Sheraz
Dependency Property Inheritance causes the value set on a parent to be applied to all of it's children (this is why setting the background on a window will make the background chang on all the children).By using the routed event, the parent will be notified that someone wants to change the current month. When the parent does so, all the children get updated.
Mike Brown
Here is what I did that solved the problem. I added a base class as you defined. Since CalendarHeader is nested in CalendarMonth, I coudn't use the DependencyProperty with Inheritance. Instead I defined a Routed Event in the base class. Raised that event in Header. Registered a handler in CalendarMonth. Casted the sender to CalendarHeader to get the value of Current Month and set e.Handled = true. That got it working.Thanks
Sheraz
Sheraz, you can pass the CurrentMonth in your routed event args. That way, you can just look there for your new value.
Mike Brown