WPF Event Handler is Null

David Marino 136 Reputation points
2021-05-13T16:00:46.983+00:00

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 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
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
745 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 110.1K Reputation points
    2021-05-13T17:47:22.567+00:00

    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” …


0 additional answers

Sort by: Most helpful