質問するログイン新規登録

回答編集履歴

2

見直しキャンペーン中

2023/07/28 14:29

投稿

TN8001
TN8001

スコア10180

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
- ```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

スコア10180

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