扩展WPF日历以支持一次选择多个日期

时间:2014-05-28 16:51:11

标签: wpf datagrid wpf-controls wpfdatagrid selecteditem

我想在WPF Calendar控件中添加一个方法,使其能够一次选择多个日期,并在结尾处仅提升SelectedDatesChangedEvent一次。

WPF Calendar控件允许您一次只添加一个日期(范围对我没用)。但是我可能需要添加大约1000个日期,我不希望SelectedDatesChangedEvent处理程序调用1000次,因为在我的情况下,这是一个昂贵的操作。

WPF DataGrid有一个非常好的功能,允许这样做:

public class MyDataGrid : DataGrid
{
    public void ClearAndSelectMany(IEnumerable<DateTime> datesToBeSelected)
    {
        this.BeginUpdateSelectedItems();
        ...
        foreach (DateTime date in datesToBeSelected)
            this.SelectedDates.Add(date);
        ...
        this.EndUpdateSelectedItems();
    }
}

但是Calendar没有像DataGrid BeginUpdateSelectedItems()那样的内容,所以我试图通过阻止base.OnSelectedDatesChanged()被调用来创建一个解决方法,直到它全部完成:

public class MyCalendar : Calendar
{
    protected override void OnSelectedDatesChanged(SelectionChangedEventArgs e)
    {
        if (false == this.temporaryDontReportSelectionChanged)
            base.OnSelectedDatesChanged(e); // this is where I get an exception
    }

    public void ClearAndSelectMany(IEnumerable<DateTime> datesToBeSelected)
    {
        this.temporaryDontReportSelectionChanged = true;
        ...
        foreach (DateTime date in datesToBeSelected)
            SelectedDates.Add(date);
        ...
        this.temporaryDontReportSelectionChanged = false;
        OnSelectedDatesChanged(
            new SelectionChangedEventArgs(
                MyCalendar.SelectedDatesChangedEvent,
                removedDates.ToList(),
                addedDates.ToList()));
    }
}

现在我的问题是我在调用base.OnSelectedDatesChanged()时遇到异常:

Unable to cast object of type
'System.EventHandler`1[System.Windows.Controls.SelectionChangedEventArgs]'
to type
'System.Windows.Controls.SelectionChangedEventHandler'.

我想我没有在接近结束时正确创建SelectionChangedEventArgs对象,但我不知道如何正确地做到这一点。任何帮助将不胜感激。

更新:在Jamleck的问题的推动下,我在一个新项目中重新创建了问题,现在可以提供更多信息。这是MyCalendar类:

public class MyCalendar : System.Windows.Controls.Calendar
{
    private bool temporaryDontReportSelectionChanged;

    public MyCalendar()
    {
        // removing this line below makes my problem go away and it works ok
        this.SelectedDatesChanged += MyCalendar_SelectedDatesChanged;
    }

    void MyCalendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {
    }

    protected override void OnSelectedDatesChanged(SelectionChangedEventArgs e)
    {
        if (!temporaryDontReportSelectionChanged)
            base.OnSelectedDatesChanged(e);
    }

    public void ClearAndSelectMany(IEnumerable<DateTime> datesToBeSelected)
    {
        this.temporaryDontReportSelectionChanged = true;
        ...
        foreach (DateTime date in datesToBeSelected)
            SelectedDates.Add(date);
        ...
        this.temporaryDontReportSelectionChanged = false;
        OnSelectedDatesChanged(
            new SelectionChangedEventArgs(
                MyCalendar.SelectedDatesChangedEvent,
                removedDates.ToList(),
                addedDates.ToList()));
    }
}

因此,如果我不向SelectedDatesChanged事件处理程序添加事件处理程序,那么一切都很好,但如果我添加它,那么我得到上面描述的InvalidCastException

0 个答案:

没有答案