回答編集履歴

1

見直しキャンペーン中

2023/07/28 14:52

投稿

TN8001
TN8001

スコア10013

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