回答編集履歴

1

文字入力時の挙動がおかしかったので、リストから選択したときのみ処理が走るように修正。

2020/11/11 06:12

投稿

ikarimame
ikarimame

スコア37

test CHANGED
@@ -76,39 +76,67 @@
76
76
 
77
77
  {
78
78
 
79
- //PART_EditableTextBox
79
+ if (sender is ComboBox combo
80
80
 
81
+ && combo.Template.FindName("PART_EditableTextBox", combo) is TextBox textbox
82
+
81
- if (sender is ComboBox combo)
83
+ && textbox != null)
82
84
 
83
85
  {
84
86
 
85
- var obj = combo.Template.FindName("PART_EditableTextBox", combo);
87
+ SelectionChangedEventHandler combo_selectionchanged = null;
86
88
 
89
+ RoutedEventHandler textbox_selectionchanged = null;
90
+
91
+ RoutedEventHandler combo_unloaded = null;
92
+
93
+
94
+
87
- if (obj is TextBox textbox)
95
+ textbox_selectionchanged = (s2, e2) =>
88
96
 
89
97
  {
90
98
 
91
- textbox.SelectionChanged += TextBox_SelectionChanged;
99
+ if (textbox.SelectionLength != 0)
92
100
 
93
- }
101
+ {
94
102
 
95
- }
103
+ textbox.SelectionChanged -= textbox_selectionchanged;
96
104
 
105
+ textbox.Select(0, 0);
106
+
97
- }
107
+ }
108
+
109
+ };
98
110
 
99
111
 
100
112
 
101
- private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
113
+ combo_selectionchanged = (s1, e1) =>
102
114
 
103
- {
115
+ {
104
116
 
105
- if (sender is TextBox tb && tb.SelectionLength != 0)
117
+ if (combo.IsDropDownOpen)
106
118
 
107
- {
119
+ textbox.SelectionChanged += textbox_selectionchanged;
108
120
 
109
- //選択されてたら解除
121
+ };
110
122
 
123
+
124
+
125
+ combo_unloaded = (s3, e3) =>
126
+
127
+ {
128
+
111
- tb.Select(tb.SelectionStart, 0);
129
+ combo.SelectionChanged -= combo_selectionchanged;
130
+
131
+ combo.Unloaded -= combo_unloaded;
132
+
133
+ };
134
+
135
+
136
+
137
+ combo.SelectionChanged += combo_selectionchanged;
138
+
139
+ combo.Unloaded += combo_unloaded;
112
140
 
113
141
  }
114
142