开发者

WPF User Control Event Firing Twice

For some reason this event fires twice. The other events I have on button clicks for example only fires once, but the date change on the date picker for what ever reason fires twice in a row.

Any ideas?

Code

public event EventHandler<CalendarEvent开发者_开发知识库Args> DateTimeStartChanged;
private void dateTimeStart_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
    if (!_loading)
    {
        e.Handled = true;

        if (DateTimeStartChanged != null)
            if (dateTimeStart.SelectedDate != null && dateTimeEnd.SelectedDate != null)
            {
                StartDate = (DateTime) dateTimeStart.SelectedDate;
                DateTimeStartChanged(this,
                                        new CalendarEventArgs((DateTime) dateTimeStart.SelectedDate,
                                                              (DateTime) dateTimeEnd.SelectedDate));
            }



    }
}


What a frustrating issue. My workaround for this issue was as follows. Has worked so far. Hope this helps someone.

private DateTime? currentDateTime = null;

private void dpDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
     if(currentDateTime != dpDate.SelectedDate)
        SaveData();
     currentDateTime = dpDate.SelectedDate;
}


It's a bug with the DatePicker control, and not your issue. Unfortunately there isn't much you can do about it except apply a workaround such as setting a flag so it only executes the first time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜