0

I have a custom control inheriting from Calendar from this project : https://www.codeproject.com/Articles/104081/Extending-the-WPF-Calendar-Control. I would like to have a DatePicker that use this calendar instead of the default one.

With the help of this topic : How to get a Calendar in DatePicker in WPF I can get the calendar of my DatePicker, the next step is to make the DatePicker open the custom calendar.

Here is my code in the MainWindow :

// Find the Popup in template
Popup MyPopup = FindChild<Popup>(MyDatePicker, "PART_Popup");
    
//Custom calendar
FsControls.FsCalendar CalendarForDatePicker = new FsControls.FsCalendar();
    
//Use custom calendar in the DatePicker
MyPopup.Child = CalendarForDatePicker;

It works and open the custom calendar when I click in the DatePicker, but the events are not linked and raised when I select a date. I don't know the right way to "connect" the events from my custom calendar to the DatePicker.

Am I missing some steps ?

leun4m
  • 560
  • 5
  • 24
  • The more appropriate approach would be to change control template or subclassing. – Sinatr Sep 21 '20 at 08:11
  • you mean to change the DatePicker template ? I checked here https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/datepicker-styles-and-templates?view=netframeworkdesktop-4.8 but I don't find a way to change the calendar used by the control. Could you give me more details of what you think ? Sorry I'm kind of a novice on WPF. – user3193210 Sep 21 '20 at 08:45
  • That would be some work to do. [Extract control template](https://stackoverflow.com/q/8825030/1997232) first, then see how you can modify it to replace calendar part. To example, [here](https://stackoverflow.com/q/19305922/1997232) someone is trying to alter calendar. – Sinatr Sep 21 '20 at 09:33
  • Thanks for the links, that's what I was trying to do, but in the DatePicker I don't find any way to point to a Calendar control, the DatePicker doesn't have a Calendar property. The only part in the template mentioning the calendar is this line : There is a "Child" property but when I set this property with the Key of a calendar I added in the window resources, it doesn't work. – user3193210 Sep 21 '20 at 10:16
  • That explains why someone was using WPFToolkit. There are other libraries around. Or you have to make own custom DatePicker control. – Sinatr Sep 21 '20 at 10:25
  • You can look into [source](https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/DatePicker.cs) and use reflection to call event handlers directly. – Sinatr Sep 21 '20 at 10:30

0 Answers0