How to restrict ClassHandler to operate on the instance of the FrameworkElement?

Emon Haque 3,176 Reputation points

I was creating a MonthPicker and noticed that Visual Studio 2019 Community (16.9.4) calls property of ViewModel before instantiating it. Here's what it does:


so here's it's gotten into the setter of QueryEditablePlot and tells that EditablePlots is null although it's been initialized in the constructor. This is the first time I've seen such an issue and it's been happening since this morning. To get rid of that error, I've to set a breakpoint in the constructor and let it hit that point and go. The problem for this question is EventManager.RegisterClassHandler in custom FrameworkElement.

I've two custom FrameworkElement, DayPicker and MonthPicker. In the constructor of DayPicker I've this:

EventManager.RegisterClassHandler(typeof(MonthYear), MonthYear.MouseUpEvent, new RoutedEventHandler(onMonthYearClicked), true);  
EventManager.RegisterClassHandler(typeof(Day), Day.MouseUpEvent, new RoutedEventHandler(onDayClicked), true);  

and in the constructor of MonthPicker this:

EventManager.RegisterClassHandler(typeof(MonthYear), MonthYear.MouseUpEvent, new RoutedEventHandler(onMonthYearClicked), true);  

both of the MonthYear and Day are custom FrameworkElement. Now, when I click on my DayPicker button to open popup and select a Day, that day becomes selected in every DayPicker in the entire application:


If I type in the date, without clicking on Day, it behaves as expected. In the MonthPicker I have MonthYear, no Day. Now when I go to decade/year mode and select a decade/year in the MonthPicker, all of my DayPicker go into decade/year mode:


How to change this behavior? I want it to affect only the DayPicker or MonthPicker I clicked.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,635 questions
{count} votes

Accepted answer
  1. Emon Haque 3,176 Reputation points

    Both Bubbling and Tunneling events work. Now instead of EventManager.RegisterClassHandler I've monthYearContainer.MouseLeftButtonUp += onMonthYearClicked; in MonthPicker and dayMonthYearContainer.MouseLeftButtonUp += onDayMonthYearClicked; in DayPicker. Both of those ...Container are Grid, no other changes were required other than checking e.Source in the handler.


    0 comments No comments

0 additional answers

Sort by: Most helpful