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

回答編集履歴

1

見直しキャンペーン中

2023/07/28 14:52

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,40 +1,40 @@
1
- > イベントトリガーとビヘイビアの使い分けに関して教えてください。
1
+ > イベントトリガーとビヘイビアの使い分けに関して教えてください。
2
-
2
+
3
- まずは普通に`Command`と`CommandParameter`での実装を考えると思うのですが、クリックなのはただの例でありほんとは違うイベントなんでしょうか?
3
+ まずは普通に`Command`と`CommandParameter`での実装を考えると思うのですが、クリックなのはただの例でありほんとは違うイベントなんでしょうか?
4
- [ButtonBase.Command プロパティ (System.Windows.Controls.Primitives) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.buttonbase.command)
4
+ [ButtonBase.Command プロパティ (System.Windows.Controls.Primitives) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.buttonbase.command)
5
- [ButtonBase.CommandParameter プロパティ (System.Windows.Controls.Primitives) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.buttonbase.commandparameter)
5
+ [ButtonBase.CommandParameter プロパティ (System.Windows.Controls.Primitives) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.primitives.buttonbase.commandparameter)
6
-
7
-
6
+
7
+
8
- ```xaml
8
+ ```xml
9
- <StackPanel>
9
+ <StackPanel>
10
- <Button
10
+ <Button
11
- Uid="0"
11
+ Uid="0"
12
- Command="{Binding BtnClick}"
12
+ Command="{Binding BtnClick}"
13
- CommandParameter="{Binding Uid, RelativeSource={RelativeSource Self}}"
13
+ CommandParameter="{Binding Uid, RelativeSource={RelativeSource Self}}"
14
- Content="btnA" />
14
+ Content="btnA" />
15
- <Button
15
+ <Button
16
- Uid="1"
16
+ Uid="1"
17
- Command="{Binding BtnClick}"
17
+ Command="{Binding BtnClick}"
18
- CommandParameter="{Binding Uid, RelativeSource={RelativeSource Self}}"
18
+ CommandParameter="{Binding Uid, RelativeSource={RelativeSource Self}}"
19
- Content="btnB" />
19
+ Content="btnB" />
20
- </StackPanel>
20
+ </StackPanel>
21
- ```
21
+ ```
22
-
22
+
23
- ```C#
23
+ ```cs
24
- public ListenerCommand<string> BtnClick { get; }
24
+ public ListenerCommand<string> BtnClick { get; }
25
- = new ListenerCommand<string>(uid => Debug.WriteLine(uid));
25
+ = new ListenerCommand<string>(uid => Debug.WriteLine(uid));
26
- ```
26
+ ```
27
-
27
+
28
- > 処理をまとめることはできるでしょうか?
28
+ > 処理をまとめることはできるでしょうか?
29
-
29
+
30
- 何度も同じことを書きたくないということであれば、
30
+ 何度も同じことを書きたくないということであれば、
31
- ```xaml
31
+ ```xml
32
- <Window.Resources>
32
+ <Window.Resources>
33
- <Style TargetType="Button">
33
+ <Style TargetType="Button">
34
- <Setter Property="Command" Value="{Binding BtnClick}" />
34
+ <Setter Property="Command" Value="{Binding BtnClick}" />
35
- <Setter Property="CommandParameter" Value="{Binding Uid, RelativeSource={RelativeSource Self}}" />
35
+ <Setter Property="CommandParameter" Value="{Binding Uid, RelativeSource={RelativeSource Self}}" />
36
- </Style>
36
+ </Style>
37
- </Window.Resources>
37
+ </Window.Resources>
38
- ```
38
+ ```
39
- のように一括指定は可能です(この場合はすべてのボタンに適用)
39
+ のように一括指定は可能です(この場合はすべてのボタンに適用)
40
40
  一部にしたいなら`Key`を指定する。