回答編集履歴
2
見直しキャンペーン中
answer
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
|
-
```
|
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
|
-
```
|
105
|
+
```cs
|
106
106
|
using Reactive.Bindings;
|
107
107
|
using System.Threading.Tasks;
|
108
108
|
using System.Windows;
|
1
糞リニューアルマークダウン崩れ修正
answer
CHANGED
@@ -1,134 +1,133 @@
|
|
1
|
-
> ダブルクリック抑制が走るとそれ以降、キーボードのフォーカスがどこかへ行ってしまい、
|
2
|
-
> キーボードでの操作ができなくなって困っています。
|
3
|
-
|
4
|
-
ボタンが一時的に無効になるので、`Window`にフォーカスが飛んでいると思います。
|
5
|
-
`Tab`キーを押せば戻ってくると思いますが、そういうことではないんですよね?
|
6
|
-
|
7
|
-
ボタンを押したらどこにフォーカスが行くのがいいんでしょうか?
|
8
|
-
|
9
|
-
1. 無効の間どこかへ行ってもいいが、有効に戻った時に取り戻す(普通のボタンと同じ使い勝手)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
`
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml
|
33
|
-
xmlns:
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
<
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
<
|
50
|
-
|
51
|
-
|
52
|
-
<RowDefinition />
|
53
|
-
|
54
|
-
<
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
<
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
using
|
108
|
-
using System.
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
public AsyncReactiveCommand
|
121
|
-
public AsyncReactiveCommand
|
122
|
-
public AsyncReactiveCommand
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
}
|
1
|
+
> ダブルクリック抑制が走るとそれ以降、キーボードのフォーカスがどこかへ行ってしまい、
|
2
|
+
> キーボードでの操作ができなくなって困っています。
|
3
|
+
|
4
|
+
ボタンが一時的に無効になるので、`Window`にフォーカスが飛んでいると思います。
|
5
|
+
`Tab`キーを押せば戻ってくると思いますが、そういうことではないんですよね?
|
6
|
+
|
7
|
+
ボタンを押したらどこにフォーカスが行くのがいいんでしょうか?
|
8
|
+
|
9
|
+
1. 無効の間どこかへ行ってもいいが、有効に戻った時に取り戻す(普通のボタンと同じ使い勝手)
|
10
|
+
`IsEnabled`を見て、`FocusedElement`を自身に設定するような感じでどうでしょう?
|
11
|
+
ほかにも有効無効が変わりがちなボタンがあると邪魔かも。
|
12
|
+
1. 無効になったんだから次のボタンに行くべき
|
13
|
+
1とは逆に相手側を見て自身をセットしましたが、わかりにくいし冗長になるしいいとこないですね^^;
|
14
|
+
やるんだったらビヘイビアでやるべきでしょうね。
|
15
|
+
|
16
|
+
|
17
|
+
`FocusedElement`はあくまで論理フォーカスなので、場合によってはキーボードフォーカスが来ないこともあります。
|
18
|
+
`Keyboard.Focus`を呼ぶほうが確実性は高いでしょうが、これもビヘイビアになるでしょうか。
|
19
|
+
|
20
|
+
|
21
|
+
[FocusManager.FocusedElement 添付プロパティ (System.Windows.Input) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.input.focusmanager.focusedelement)
|
22
|
+
|
23
|
+
[Keyboard.Focus(IInputElement) メソッド (System.Windows.Input) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.input.keyboard.focus)
|
24
|
+
|
25
|
+
[フォーカスの概要 - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/focus-overview)
|
26
|
+
|
27
|
+
|
28
|
+
```xaml
|
29
|
+
<Window
|
30
|
+
x:Class="Questions351448.MainWindow"
|
31
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
32
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
33
|
+
xmlns:local="clr-namespace:Questions351448"
|
34
|
+
Width="400"
|
35
|
+
Height="300"
|
36
|
+
FocusManager.FocusedElement="{Binding ElementName=button1}">
|
37
|
+
<Window.DataContext>
|
38
|
+
<local:MainWindowViewModel />
|
39
|
+
</Window.DataContext>
|
40
|
+
<Window.Resources>
|
41
|
+
<Style x:Key="SelfFocusStyle" TargetType="Button">
|
42
|
+
<Style.Triggers>
|
43
|
+
<Trigger Property="IsEnabled" Value="True">
|
44
|
+
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
|
45
|
+
</Trigger>
|
46
|
+
</Style.Triggers>
|
47
|
+
</Style>
|
48
|
+
</Window.Resources>
|
49
|
+
<Grid>
|
50
|
+
<Grid.RowDefinitions>
|
51
|
+
<RowDefinition />
|
52
|
+
<RowDefinition />
|
53
|
+
</Grid.RowDefinitions>
|
54
|
+
<GroupBox Header="1. 自身に再設定">
|
55
|
+
<StackPanel>
|
56
|
+
<Button
|
57
|
+
x:Name="button1"
|
58
|
+
Command="{Binding Command1}"
|
59
|
+
Content="Button1"
|
60
|
+
Style="{StaticResource SelfFocusStyle}" />
|
61
|
+
<Button
|
62
|
+
x:Name="button2"
|
63
|
+
Command="{Binding Command2}"
|
64
|
+
Content="Button2"
|
65
|
+
Style="{StaticResource SelfFocusStyle}" />
|
66
|
+
</StackPanel>
|
67
|
+
</GroupBox>
|
68
|
+
|
69
|
+
<GroupBox Grid.Row="1" Header="2. 次のボタン">
|
70
|
+
<StackPanel>
|
71
|
+
<Button
|
72
|
+
x:Name="button3"
|
73
|
+
Command="{Binding Command3}"
|
74
|
+
Content="Button3">
|
75
|
+
<Button.Style>
|
76
|
+
<Style TargetType="Button">
|
77
|
+
<Style.Triggers>
|
78
|
+
<DataTrigger Binding="{Binding IsEnabled, ElementName=button4}" Value="False">
|
79
|
+
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
|
80
|
+
</DataTrigger>
|
81
|
+
</Style.Triggers>
|
82
|
+
</Style>
|
83
|
+
</Button.Style>
|
84
|
+
</Button>
|
85
|
+
<Button
|
86
|
+
x:Name="button4"
|
87
|
+
Command="{Binding Command4}"
|
88
|
+
Content="Button4">
|
89
|
+
<Button.Style>
|
90
|
+
<Style TargetType="Button">
|
91
|
+
<Style.Triggers>
|
92
|
+
<DataTrigger Binding="{Binding IsEnabled, ElementName=button3}" Value="False">
|
93
|
+
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}" />
|
94
|
+
</DataTrigger>
|
95
|
+
</Style.Triggers>
|
96
|
+
</Style>
|
97
|
+
</Button.Style>
|
98
|
+
</Button>
|
99
|
+
</StackPanel>
|
100
|
+
</GroupBox>
|
101
|
+
</Grid>
|
102
|
+
</Window>
|
103
|
+
```
|
104
|
+
|
105
|
+
```C#
|
106
|
+
using Reactive.Bindings;
|
107
|
+
using System.Threading.Tasks;
|
108
|
+
using System.Windows;
|
109
|
+
|
110
|
+
namespace Questions351448
|
111
|
+
{
|
112
|
+
public partial class MainWindow : Window
|
113
|
+
{
|
114
|
+
public MainWindow() => InitializeComponent();
|
115
|
+
}
|
116
|
+
|
117
|
+
class MainWindowViewModel
|
118
|
+
{
|
119
|
+
public AsyncReactiveCommand Command1 { get; } = new AsyncReactiveCommand();
|
120
|
+
public AsyncReactiveCommand Command2 { get; } = new AsyncReactiveCommand();
|
121
|
+
public AsyncReactiveCommand Command3 { get; } = new AsyncReactiveCommand();
|
122
|
+
public AsyncReactiveCommand Command4 { get; } = new AsyncReactiveCommand();
|
123
|
+
|
124
|
+
public MainWindowViewModel()
|
125
|
+
{
|
126
|
+
Command1.Subscribe(async () => await Task.Delay(500));
|
127
|
+
Command2.Subscribe(async () => await Task.Delay(500));
|
128
|
+
Command3.Subscribe(async () => await Task.Delay(500));
|
129
|
+
Command4.Subscribe(async () => await Task.Delay(500));
|
130
|
+
}
|
131
|
+
}
|
132
|
+
}
|
134
133
|
```
|