Question

I have a custom calendar control which associates text with a date, instead of a DatePicker style of control. This custom control has a class for Day with property Notes:

public string Notes
    {
        get { return notes; }
        set
        {
            notes = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Notes"));
        }
    }

On the actual Calendar class, I have an event handler:

public event EventHandler<DayChangedEventArgs> DayChanged;

I have a Day_Changed method which triggers the event:

private void Day_Changed(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName != "Notes") return;
        if (DayChanged == null) return;

        DayChanged(this, new DayChangedEventArgs(sender as Day));
    }

public class DayChangedEventArgs : EventArgs
{
    public Day Day { get; private set; }

    public DayChangedEventArgs(Day day)
    {
        this.Day = day;
    }
}

And then on the actual form, I can use this created event DayChanged to trigger a method behind the form:

 <PersonnelTracker:Calendar x:Name="Calendar" DayChanged="Calendar_DayChanged"/>

 private void Calendar_DayChanged(object sender, DayChangedEventArgs e)
    {

    }

Now, what I am looking to do, is to find a way to be able to select a day or position on the calendar and trigger it to a method behind the form. The complication I am having, is that the calendar control itself doesn't have a click event, meaning one would probably have to be made, yet I can't use MouseButtonEventArgs, due to not having an individual object to work with, such as a button.

Is there any suggestion on how best to do this?

Calendar Example So this is the Calendar, and for those who don't understand, I just need to click on a certain day and it to set off a click event, yet like I stated before, this calendar control doesn't have one yet, and therefore I am looking for the best possible way to create such.

The only extra details I can put forward, are that you can go through each day item in turn such like this:

foreach (Day item in Calendar.Days)
            {

            }
Was it helpful?

Solution

Ok, so the way that I see this working is simple. Either, you use a Button for each 'day' in the calendar, or you use one of the PreviewMouseDown events on whatever element each day is currently composed of.

Personally, I'd rather go with the Button, or thinking about it further, a ToggleButton would be even better. The ToggleButton has the IsChecked property which you could bind to the Background property using a converter, or maybe even just a Trigger. That way, you could easily highlight the selected day.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top