|
1 |
| -namespace MaterialDesignThemes.Wpf.Internal |
| 1 | +namespace MaterialDesignThemes.Wpf.Internal; |
| 2 | + |
| 3 | +public static class ClearText |
2 | 4 | {
|
3 |
| - public static class ClearText |
4 |
| - { |
5 |
| - public static readonly RoutedCommand ClearCommand = new(); |
| 5 | + public static readonly RoutedCommand ClearCommand = new(); |
6 | 6 |
|
7 |
| - public static bool GetHandlesClearCommand(DependencyObject obj) |
8 |
| - => (bool)obj.GetValue(HandlesClearCommandProperty); |
| 7 | + public static bool GetHandlesClearCommand(DependencyObject obj) |
| 8 | + => (bool)obj.GetValue(HandlesClearCommandProperty); |
9 | 9 |
|
10 |
| - public static void SetHandlesClearCommand(DependencyObject obj, bool value) |
11 |
| - => obj.SetValue(HandlesClearCommandProperty, value); |
| 10 | + public static void SetHandlesClearCommand(DependencyObject obj, bool value) |
| 11 | + => obj.SetValue(HandlesClearCommandProperty, value); |
12 | 12 |
|
13 |
| - public static readonly DependencyProperty HandlesClearCommandProperty = |
14 |
| - DependencyProperty.RegisterAttached("HandlesClearCommand", typeof(bool), typeof(ClearText), new PropertyMetadata(false, OnHandlesClearCommandChanged)); |
| 13 | + public static readonly DependencyProperty HandlesClearCommandProperty = |
| 14 | + DependencyProperty.RegisterAttached("HandlesClearCommand", typeof(bool), typeof(ClearText), new PropertyMetadata(false, OnHandlesClearCommandChanged)); |
15 | 15 |
|
16 |
| - private static void OnHandlesClearCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 16 | + private static void OnHandlesClearCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 17 | + { |
| 18 | + if (d is UIElement element) |
17 | 19 | {
|
18 |
| - if (d is UIElement element) |
| 20 | + if ((bool)e.NewValue) |
19 | 21 | {
|
20 |
| - if ((bool)e.NewValue) |
21 |
| - { |
22 |
| - element.CommandBindings.Add(new CommandBinding(ClearCommand, OnClearCommand)); |
23 |
| - } |
24 |
| - else |
| 22 | + element.CommandBindings.Add(new CommandBinding(ClearCommand, OnClearCommand)); |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + for (int i = element.CommandBindings.Count - 1; i >= 0; i--) |
25 | 27 | {
|
26 |
| - for (int i = element.CommandBindings.Count - 1; i >= 0; i--) |
| 28 | + if (element.CommandBindings[i].Command == ClearCommand) |
27 | 29 | {
|
28 |
| - if (element.CommandBindings[i].Command == ClearCommand) |
29 |
| - { |
30 |
| - element.CommandBindings.RemoveAt(i); |
31 |
| - } |
| 30 | + element.CommandBindings.RemoveAt(i); |
32 | 31 | }
|
33 | 32 | }
|
34 | 33 | }
|
| 34 | + } |
35 | 35 |
|
36 |
| - static void OnClearCommand(object sender, ExecutedRoutedEventArgs e) |
| 36 | + static void OnClearCommand(object sender, ExecutedRoutedEventArgs e) |
| 37 | + { |
| 38 | + switch (sender) |
37 | 39 | {
|
38 |
| - switch (sender) |
39 |
| - { |
40 |
| - case DatePicker datePicker: |
41 |
| - datePicker.SetCurrentValue(DatePicker.SelectedDateProperty, null); |
42 |
| - break; |
43 |
| - case TextBox textBox: |
44 |
| - textBox.SetCurrentValue(TextBox.TextProperty, null); |
45 |
| - break; |
46 |
| - case ComboBox comboBox: |
47 |
| - comboBox.SetCurrentValue(ComboBox.TextProperty, null); |
48 |
| - comboBox.SetCurrentValue(Selector.SelectedItemProperty, null); |
49 |
| - break; |
50 |
| - case PasswordBox passwordBox: |
51 |
| - passwordBox.Password = null; |
52 |
| - break; |
53 |
| - } |
54 |
| - e.Handled = true; |
| 40 | + case DatePicker datePicker: |
| 41 | + datePicker.SetCurrentValue(DatePicker.SelectedDateProperty, null); |
| 42 | + break; |
| 43 | + case TextBox textBox: |
| 44 | + textBox.SetCurrentValue(TextBox.TextProperty, null); |
| 45 | + break; |
| 46 | + case ComboBox comboBox: |
| 47 | + comboBox.SetCurrentValue(ComboBox.TextProperty, null); |
| 48 | + comboBox.SetCurrentValue(Selector.SelectedItemProperty, null); |
| 49 | + break; |
| 50 | + case PasswordBox passwordBox: |
| 51 | + passwordBox.Password = null; |
| 52 | + break; |
55 | 53 | }
|
| 54 | + e.Handled = true; |
56 | 55 | }
|
57 |
| - |
58 | 56 | }
|
| 57 | + |
59 | 58 | }
|
0 commit comments