Skip to content

Commit 764bc09

Browse files
committed
1 parent 1449e99 commit 764bc09

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/WPFDevelopers.Shared/Core/Helpers/ElementHelper.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections;
23
using System.Windows;
34
using System.Windows.Controls;
5+
using System.Windows.Controls.Primitives;
46
using WPFDevelopers.Controls;
57

68
namespace WPFDevelopers.Helpers
@@ -91,25 +93,32 @@ private static void OnIsClearChanged(DependencyObject d, DependencyPropertyChang
9193

9294
private static void OnButtonClear_Click(object sender, RoutedEventArgs e)
9395
{
94-
if (sender is Button button)
96+
if (sender is Button button && button.TemplatedParent != null)
9597
{
96-
if (button.TemplatedParent is TextBox textBox)
97-
textBox.Clear();
98-
else if (button.TemplatedParent is PasswordBox passwordBox)
99-
passwordBox.Clear();
100-
else if (button.TemplatedParent is TabItem tabItem)
98+
switch (button.TemplatedParent)
10199
{
102-
var tabControl = tabItem.Parent as TabControl;
103-
if (tabControl != null)
104-
tabControl.Items.Remove(tabItem);
105-
}
106-
else if(button.TemplatedParent is DateRangePicker dateRangePicker)
107-
{
108-
dateRangePicker.StartDate = null;
109-
dateRangePicker.EndDate = null;
100+
case TextBox textBox:
101+
textBox.Clear();
102+
break;
103+
case PasswordBox passwordBox:
104+
passwordBox.Clear();
105+
break;
106+
case TabItem tabItem:
107+
var tabControl = ControlsHelper.FindParent<TabControl>(tabItem);
108+
if (tabControl != null)
109+
{
110+
if (tabControl.ItemsSource is IList itemsSource)
111+
itemsSource.Remove(tabItem.DataContext);
112+
else
113+
tabControl.Items.Remove(tabItem);
114+
}
115+
break;
116+
case DateRangePicker dateRangePicker:
117+
dateRangePicker.StartDate = null;
118+
dateRangePicker.EndDate = null;
119+
break;
110120
}
111121
}
112122
}
113-
114123
}
115124
}

0 commit comments

Comments
 (0)