1
1
using System ;
2
+ using System . Collections ;
2
3
using System . Windows ;
3
4
using System . Windows . Controls ;
5
+ using System . Windows . Controls . Primitives ;
4
6
using WPFDevelopers . Controls ;
5
7
6
8
namespace WPFDevelopers . Helpers
@@ -91,25 +93,32 @@ private static void OnIsClearChanged(DependencyObject d, DependencyPropertyChang
91
93
92
94
private static void OnButtonClear_Click ( object sender , RoutedEventArgs e )
93
95
{
94
- if ( sender is Button button )
96
+ if ( sender is Button button && button . TemplatedParent != null )
95
97
{
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 )
101
99
{
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 ;
110
120
}
111
121
}
112
122
}
113
-
114
123
}
115
124
}
0 commit comments