Skip to content

Commit cfc63f7

Browse files
committed
Fix issue(#140
Fix issue(#140
1 parent dde3e3f commit cfc63f7

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/WPFDevelopers.Samples.Shared/ExampleViews/DateRangePickerExample.xaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
EndDate="{Binding EndDate, RelativeSource={RelativeSource AncestorType=local:DateRangePickerExample}}"
3333
EndWatermark="结束日期"
3434
StartDate="{Binding StartDate, RelativeSource={RelativeSource AncestorType=local:DateRangePickerExample}}"
35-
StartWatermark="开始日期" />
35+
StartWatermark="开始日期"
36+
wd:ElementHelper.IsClear="True"/>
3637
<Button
3738
Width="60"
3839
HorizontalAlignment="Center"

src/WPFDevelopers.Shared/Controls/DateRangePicker/DateRangePicker.cs

+44
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,22 @@ public string DateFormat
103103

104104
private static void OnStartDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
105105
{
106+
var ctrl = d as DateRangePicker;
107+
if (ctrl != null && ctrl._textBoxStart != null)
108+
{
109+
ctrl._textBoxStart.Text = e.NewValue?.ToString();
110+
ctrl.UpdateStartDateFromTextBox();
111+
}
106112
}
107113

108114
private static void OnEndDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
109115
{
116+
var ctrl = d as DateRangePicker;
117+
if (ctrl != null && ctrl._textBoxEnd != null)
118+
{
119+
ctrl._textBoxEnd.Text = e.NewValue?.ToString();
120+
ctrl.UpdateEndDateFromTextBox();
121+
}
110122
}
111123

112124

@@ -171,10 +183,29 @@ public override void OnApplyTemplate()
171183
Loaded += DateRangePicker_Loaded;
172184
}
173185

186+
private void OnBorder_PreviewMouseUp(object sender, MouseButtonEventArgs e)
187+
{
188+
if (e.OriginalSource is Button button)
189+
return;
190+
if (_popup != null && !_popup.IsOpen)
191+
_popup.IsOpen = true;
192+
}
193+
174194
private void TextBoxEnd_TextChanged(object sender, TextChangedEventArgs e)
195+
{
196+
UpdateEndDateFromTextBox();
197+
}
198+
199+
void UpdateEndDateFromTextBox()
175200
{
176201
if (_textBoxEnd != null)
177202
{
203+
if (string.IsNullOrWhiteSpace(_textBoxEnd.Text))
204+
{
205+
_endDate = null;
206+
SetIsHighlightFalse(_startCalendarDayButtons);
207+
return;
208+
}
178209
if (DateTime.TryParse(_textBoxEnd.Text, out var dateTime))
179210
{
180211
if (EndDate.HasValue && dateTime.ToString(DateFormat) == EndDate.Value.ToString(DateFormat))
@@ -199,9 +230,20 @@ private void TextBoxEnd_TextChanged(object sender, TextChangedEventArgs e)
199230
}
200231

201232
private void TextBoxStart_TextChanged(object sender, TextChangedEventArgs e)
233+
{
234+
UpdateStartDateFromTextBox();
235+
}
236+
237+
void UpdateStartDateFromTextBox()
202238
{
203239
if (_textBoxStart != null)
204240
{
241+
if (string.IsNullOrWhiteSpace(_textBoxStart.Text))
242+
{
243+
_startDate = null;
244+
SetIsHighlightFalse(_startCalendarDayButtons);
245+
return;
246+
}
205247
if (DateTime.TryParse(_textBoxStart.Text, out var dateTime))
206248
{
207249
if (StartDate.HasValue && dateTime.ToString(DateFormat) == StartDate.Value.ToString(DateFormat))
@@ -357,6 +399,8 @@ private void OnWindow_MouseDown(object sender, MouseButtonEventArgs e)
357399

358400
private void OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
359401
{
402+
if (e.OriginalSource is Button button)
403+
return;
360404
if (_popup != null && !_popup.IsOpen)
361405
_popup.IsOpen = true;
362406
}

0 commit comments

Comments
 (0)