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….> }