dotnet / dotnet/wpf

DatePicker wrongly raises SelectedDateChanged event twice with manual date entry

Open
#2,608 2 comments 3 reactions 0 assignees View on GitHub
Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: 3.1
* Does the bug reproduce also in WPF for .NET Framework 4.7.2: Yes
* Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No

**Problem description:**
When clicking on a date in the calendar, DatePicker.SelectedDateChanged events gets correctly raised once.
When entering the date manually in the DatePicker's TextBox, DatePicker.SelectedDateChanged events gets wrongly raised twice with the same event data.

**Actual behavior:**
When entering the date manually in the TextBox, DatePicker.SelectedDateChanged events gets wrongly raised twice with the same event data.

**Expected behavior:**
When entering the date manually in the TextBox, DatePicker.SelectedDateChanged events gets raised once.

**Minimal repro:**
Create WPF app with the code below.
1) Select a date in the calendar. The TextBlock will show one line with the added date
2) Select another date in the calendar. The TextBlock will show a new line with the added date and the removed date
3) Enter a date manually and press Enter. The TextBlock will show **2** new lines, both with the same data (added and removed date), but slightly different time (minute:second millisec).

```
TimeStamp SelectedDate
04:43 522 01.02.2020 added 1.2.20 00:00:00;
04:53 402 02.02.2020 added 2.2.20 00:00:00; removed 1.2.20 00:00:00;
05:05 059 03.02.2020 added 3.2.20 00:00:00; removed 2.2.20 00:00:00;
05:05 064 03.02.2020 added 3.2.20 00:00:00; removed 2.2.20 00:00:00;
```

```xml.xaml




```

```C#
using System;
using System.Windows;
using System.Windows.Controls;

namespace DatePickerDoubleEventRaiseBug {
public partial class MainWindow: Window {
public MainWindow() {
InitializeComponent();
TestDatePicker.SelectedDateChanged+=TestDatePicker_SelectedDateChanged;
}

private void TestDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e) {
var itemsString = "";
foreach (var item in e.AddedItems) {
itemsString += "added " + item + "; ";
}
foreach (var item in e.RemovedItems) {
itemsString += "removed " + item + "; ";
}
TraceTextBlock.Text += $"{DateTime.Now.ToString("mm:ss fff")} {TestDatePicker.SelectedDate?.ToString("dd.MM.yyyy")} {itemsString}" +
Environment.NewLine;
}
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.