回答編集履歴

2

見直しキャンペーン中

2023/07/28 14:29

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -25,7 +25,7 @@
25
25
  [フォーカスの概要 - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/focus-overview)
26
26
 
27
27
 
28
- ```xaml
28
+ ```xml
29
29
  <Window
30
30
  x:Class="Questions351448.MainWindow"
31
31
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
@@ -102,7 +102,7 @@
102
102
  </Window>
103
103
  ```
104
104
 
105
- ```C#
105
+ ```cs
106
106
  using Reactive.Bindings;
107
107
  using System.Threading.Tasks;
108
108
  using System.Windows;

1

糞リニューアルマークダウン崩れ修正

2022/09/20 08:29

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,267 +1,133 @@
1
1
  > ダブルクリック抑制が走るとそれ以降、キーボードのフォーカスがどこかへ行ってしまい、
2
-
3
2
  > キーボードでの操作ができなくなって困っています。
4
3
 
5
-
6
-
7
4
  ボタンが一時的に無効になるので、`Window`にフォーカスが飛んでいると思います。
8
-
9
5
  `Tab`キーを押せば戻ってくると思いますが、そういうことではないんですよね?
10
-
11
-
12
6
 
13
7
  ボタンを押したらどこにフォーカスが行くのがいいんでしょうか?
14
8
 
15
-
16
-
17
9
  1. 無効の間どこかへ行ってもいいが、有効に戻った時に取り戻す(普通のボタンと同じ使い勝手)
18
-
19
- `IsEnabled`を見て、`FocusedElement`を自身に設定するような感じでどうでしょう?
10
+ `IsEnabled`を見て、`FocusedElement`を自身に設定するような感じでどうでしょう?
20
-
21
- ほかにも有効無効が変わりがちなボタンがあると邪魔かも。
11
+ ほかにも有効無効が変わりがちなボタンがあると邪魔かも。
22
-
23
-
24
-
25
- 2. 無効になったんだから次のボタンに行くべき
12
+ 1. 無効になったんだから次のボタンに行くべき
26
-
27
- 1とは逆に相手側を見て自身をセットしましたが、わかりにくいし冗長になるしいいとこないですね^^;
13
+ 1とは逆に相手側を見て自身をセットしましたが、わかりにくいし冗長になるしいいとこないですね^^;
28
-
29
14
  やるんだったらビヘイビアでやるべきでしょうね。
30
15
 
31
16
 
32
-
33
-
34
-
35
17
  `FocusedElement`はあくまで論理フォーカスなので、場合によってはキーボードフォーカスが来ないこともあります。
36
-
37
18
  `Keyboard.Focus`を呼ぶほうが確実性は高いでしょうが、これもビヘイビアになるでしょうか。
38
-
39
-
40
-
41
19
 
42
20
 
43
21
  [FocusManager.FocusedElement 添付プロパティ (System.Windows.Input) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.input.focusmanager.focusedelement)
44
22
 
45
-
46
-
47
23
  [Keyboard.Focus(IInputElement) メソッド (System.Windows.Input) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.input.keyboard.focus)
48
-
49
-
50
24
 
51
25
  [フォーカスの概要 - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/focus-overview)
52
26
 
53
27
 
54
-
55
-
56
-
57
28
  ```xaml
58
-
59
29
  <Window
60
-
61
30
  x:Class="Questions351448.MainWindow"
62
-
63
31
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
64
-
65
32
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
66
-
67
33
  xmlns:local="clr-namespace:Questions351448"
68
-
69
34
  Width="400"
70
-
71
35
  Height="300"
72
-
73
36
  FocusManager.FocusedElement="{Binding ElementName=button1}">
74
-
75
37
  <Window.DataContext>
76
-
77
38
  <local:MainWindowViewModel />
78
-
79
39
  </Window.DataContext>
80
-
81
40
  <Window.Resources>
82
-
83
41
  <Style x:Key="SelfFocusStyle" TargetType="Button">
84
-
85
42
  <Style.Triggers>
86
-
87
43
  <Trigger Property="IsEnabled" Value="True">
88
-
89
44
  <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
90
-
91
45
  </Trigger>
92
-
93
46
  </Style.Triggers>
94
-
95
47
  </Style>
96
-
97
48
  </Window.Resources>
98
-
99
49
  <Grid>
100
-
101
50
  <Grid.RowDefinitions>
102
-
103
51
  <RowDefinition />
104
-
105
52
  <RowDefinition />
106
-
107
53
  </Grid.RowDefinitions>
108
-
109
54
  <GroupBox Header="1. 自身に再設定">
110
-
111
55
  <StackPanel>
112
-
113
56
  <Button
114
-
115
57
  x:Name="button1"
116
-
117
58
  Command="{Binding Command1}"
118
-
119
59
  Content="Button1"
120
-
121
60
  Style="{StaticResource SelfFocusStyle}" />
122
-
123
61
  <Button
124
-
125
62
  x:Name="button2"
126
-
127
63
  Command="{Binding Command2}"
128
-
129
64
  Content="Button2"
130
-
131
65
  Style="{StaticResource SelfFocusStyle}" />
132
-
133
66
  </StackPanel>
134
-
135
67
  </GroupBox>
136
68
 
137
-
138
-
139
69
  <GroupBox Grid.Row="1" Header="2. 次のボタン">
140
-
141
70
  <StackPanel>
142
-
143
71
  <Button
144
-
145
72
  x:Name="button3"
146
-
147
73
  Command="{Binding Command3}"
148
-
149
74
  Content="Button3">
150
-
151
75
  <Button.Style>
152
-
153
76
  <Style TargetType="Button">
154
-
155
77
  <Style.Triggers>
156
-
157
78
  <DataTrigger Binding="{Binding IsEnabled, ElementName=button4}" Value="False">
158
-
159
79
  <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
160
-
161
80
  </DataTrigger>
162
-
163
81
  </Style.Triggers>
164
-
165
82
  </Style>
166
-
167
83
  </Button.Style>
168
-
169
84
  </Button>
170
-
171
85
  <Button
172
-
173
86
  x:Name="button4"
174
-
175
87
  Command="{Binding Command4}"
176
-
177
88
  Content="Button4">
178
-
179
89
  <Button.Style>
180
-
181
90
  <Style TargetType="Button">
182
-
183
91
  <Style.Triggers>
184
-
185
92
  <DataTrigger Binding="{Binding IsEnabled, ElementName=button3}" Value="False">
186
-
187
93
  <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
188
-
189
94
  </DataTrigger>
190
-
191
95
  </Style.Triggers>
192
-
193
96
  </Style>
194
-
195
97
  </Button.Style>
196
-
197
98
  </Button>
198
-
199
99
  </StackPanel>
200
-
201
100
  </GroupBox>
202
-
203
101
  </Grid>
204
-
205
102
  </Window>
206
-
207
103
  ```
208
104
 
209
-
210
-
211
105
  ```C#
212
-
213
106
  using Reactive.Bindings;
214
-
215
107
  using System.Threading.Tasks;
216
-
217
108
  using System.Windows;
218
109
 
219
-
220
-
221
110
  namespace Questions351448
222
-
223
111
  {
224
-
225
112
  public partial class MainWindow : Window
226
-
227
113
  {
228
-
229
114
  public MainWindow() => InitializeComponent();
230
-
231
115
  }
232
116
 
233
-
234
-
235
117
  class MainWindowViewModel
236
-
237
118
  {
238
-
239
119
  public AsyncReactiveCommand Command1 { get; } = new AsyncReactiveCommand();
240
-
241
120
  public AsyncReactiveCommand Command2 { get; } = new AsyncReactiveCommand();
242
-
243
121
  public AsyncReactiveCommand Command3 { get; } = new AsyncReactiveCommand();
244
-
245
122
  public AsyncReactiveCommand Command4 { get; } = new AsyncReactiveCommand();
246
123
 
247
-
248
-
249
124
  public MainWindowViewModel()
250
-
251
125
  {
252
-
253
126
  Command1.Subscribe(async () => await Task.Delay(500));
254
-
255
127
  Command2.Subscribe(async () => await Task.Delay(500));
256
-
257
128
  Command3.Subscribe(async () => await Task.Delay(500));
258
-
259
129
  Command4.Subscribe(async () => await Task.Delay(500));
260
-
261
130
  }
262
-
263
131
  }
264
-
265
132
  }
266
-
267
133
  ```