question

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 asked EmonHaque-1485 answered

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

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:

95528-1.gif

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:

95498-2.gif

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:

95572-3.gif

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

windows-wpf
1.gif (1.4 MiB)
2.gif (2.8 MiB)
3.gif (2.5 MiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@EmonHaque-1485
Why do you put EventManager.RegisterClassHandler(typeof(MonthYear), MonthYear.MouseUpEvent, new RoutedEventHandler(onMonthYearClicked), true); in your constructor of DayPicker? Could you give me some descrition of it?

0 Votes 0 ·

@DaisyTian-MSFT, both DayPicker and MonthPicker have decade and year mode. If I want to go to a particular year/decade in both DayPicker and MonthPicker I can. The reason I've used EventManager.RegisterClassHandler is: these Day/MonthYear FrameworkElements are created dynamically, depending on which month/year/decade I am on, as I interact with DayPicker/Monthpicker. For example:

EDIT
95890-test.gif

when I select 6th May 2014 in Lease section, the same day gets selected in the DayPicker of Transaction section and and the year in MonthPicker of Rent section becomes 2014 and when I select 5th April 2003 in Transaction section it changes the year in MonthPicker of Rent section and selected date of Lease section.

0 Votes 0 ·
test.gif (1.9 MiB)

1 Answer

EmonHaque-1485 avatar image
0 Votes"
EmonHaque-1485 answered

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.

95987-test.gif


test.gif (2.4 MiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.