question

DavidMarino-1836 avatar image
0 Votes"
DavidMarino-1836 asked DavidMarino-1836 commented

WPF Event Handler is Null

Framework 4.7.2
Original Code: Code Project – (VB) Quick and Simple WPF Month – View Calendar by Kirk Aiya
URL:[Quick-and-Simple-WPF-Month-view-Calendar-Updated][1]

I’m relatively new to C# & WPF, and in order to learn WPF / C#, I’m attempting to port Kirk’s VB calendar to C#. I have defined an event in the MonthView file, and subscribing to it in the MainWindow file, unfortunately the event is always null.

I’m not sure if I’m subscribing to the event before it is created or I’m just going about it the wrong way. My goal is to move the calendar view to the next month once I click on the button, which runs a method that invokes UpdateMonth.

Greatly appreciate any guidance / pointers that can provided.



MonthView Class :
public partial class MonthView : UserControl {…..
Event Handler Def:
public delegate void DisplayMonthChangedHandler(MonthChangedEventArgs args);
public event DisplayMonthChangedHandler DisplayedMonthChanged;

 *Invoke Event:*
 private void UpdateMonth(int MonthsToAdd)
         {
             var ev = new MonthChangedEventArgs();
             ev.OldDisplayStartDate = _DisplayStartDate;
             this.DisplayStartDate = _DisplayStartDate.AddMonths(MonthsToAdd);
             ev.NewDisplayStartDate = _DisplayStartDate;
             onDisplayedMonthChanged(ev);           
         }
    
 *Raise Event:*
         protected virtual void onDisplayedMonthChanged(MonthChangedEventArgs structMonthChange)
         {
             if (DisplayedMonthChanged != null)
             {
                 DisplayedMonthChanged(structMonthChange);
             }
         }
    
 *MonthChangedEventArgs Def:*
 public struct MonthChangedEventArgs
     {
         public DateTime OldDisplayStartDate;
         public DateTime NewDisplayStartDate;
     }

 **MainWindow**: *public partial class MainWindow : Window {…*.
 public MainWindow() 
         {InitializeComponent();}
    
 Subscribing to Event in MainWindow_Loaded
 private void MainWindow_Loaded(object sender, RoutedEventArgs e)
         {     MonthView mv = new MonthView();
             mv.DisplayedMonthChanged += DisplayMonthChanged;            
             }
 private void DisplayMonthChanged(MonthChangedEventArgs e)
         {  <code….> }
windows-wpfdotnet-wpf-xaml
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.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered DavidMarino-1836 commented

Try removing ‘MonthView mv = new MonthView()’ and make sure that the name of the element is “mv”; something like this (in XAML):

<cal:MonthView x:Name="mv" …

Instead of “+=”, you can define the event handler in XAML:

<cal:MonthView x:Name="mv" DisplayedMonthChanged=”DisplayMonthChanged” …


· 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.

Providing the XAML for better clarity:

MonthView & MainWindow XAML

[1]: /answers/storage/attachments/96424-monthview.txt

[2]: /answers/storage/attachments/96469-mainwindow.txt



The Button Click invokes the nxtMonth_OnClick method which passes a 1 or -1 to the UpdateMonth method; depending if you are moving forward or backwards on the calendar. All the documentation I have read always points to subscribing to an event using the += operator, it is not clear to me how I would subscribe to the event; unless in the background any method defined as "DisplayMonthChanged" gets executed regardless where it is defined.

I will attempt to use your suggestion, thanks for the insight.

 <Button x:Name="NextMonth"  Height="23" Width="23" VerticalAlignment="Center" Margin="6,0,6,0" Background="AliceBlue" BorderBrush="Red" Click="nxtMonth_OnClick" >

</Button>


0 Votes 0 ·
monthview.txt (3.5 KiB)
mainwindow.txt (959 B)

Update: Your recommendation worked.
I modified the MainWindow XAML to incorporate your suggestion, keeping everything else the same.

I'm not sure why it works or how I would incorporate the other events I have defined, but thanks to your recommendation I'm further along.
Any documentation / explanation as to why it is working is greatly appreciated; versus using the += operator.

Thank you for your help.

<cal:MonthView x:Name="AptCalendar" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch" DisplayedMonthChanged="DisplayMonthChanged"/>

0 Votes 0 ·