开发者

WPF User Control Event Firing Twice

开发者 https://www.devze.com 2023-02-10 07:22 出处:网络
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.

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

精彩评论

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