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

回答編集履歴

1

見直しキャンペーン中

2023/07/27 14:48

投稿

TN8001
TN8001

スコア10210

answer CHANGED
@@ -1,67 +1,66 @@
1
- `MainWrapPanel`の子だけを検知できればいいのなら、`WrapPanel`を継承し`OnVisualChildrenChanged`をオーバーライドすればいいでしょう(試したことのリンク記事はそういう意味です)
2
-
3
- ですがやりたいことって恐らくそういう意味じゃないんですよね?
4
- どこに置いたボタン(等)でもイベントを拾いたいって意味ですよね。
5
-
6
- WPFはルーティングイベントという大変便利な仕組みがあります。
7
- `MainWindow`で一括して`Button.Click`を拾うことができます。
8
-
9
- [ルーティング イベントの概要 - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/routed-events-overview)
10
-
11
- イベントによっては(例えば`Button.MouseDown`)処理済みになって来ないので、トンネルイベント(`Previewなんちゃら`)を使用してください。
12
-
13
- ```xaml
14
- <Window
15
- x:Class="Questions342011.MainWindow"
16
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
17
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18
- Width="800"
19
- Height="450"
20
- Button.Click="Window_Button_Click">
21
- <WrapPanel Name="MainWrapPanel">
22
- <Button
23
- Width="75"
24
- Margin="10"
25
- Click="AddButton_Click"
26
- Content="Add" />
27
- </WrapPanel>
28
- </Window>
29
- ```
30
-
31
- ```C#
32
- using System.Diagnostics;
33
- using System.Windows;
34
- using System.Windows.Controls;
35
-
36
- namespace Questions342011
37
- {
38
- public partial class MainWindow : Window
39
- {
40
- public MainWindow() => InitializeComponent();
41
-
42
- private int count;
43
- private void AddButton_Click(object sender, RoutedEventArgs e)
44
- {
45
- var button = new Button()
46
- {
47
- Content = $"Button{++count}",
48
- Width = 75,
49
- Margin = new Thickness(10),
50
- };
51
- button.Click += Button_Click;
52
- MainWrapPanel.Children.Add(button);
53
- }
54
-
55
- private void Button_Click(object sender, RoutedEventArgs e)
56
- {
57
- Debug.WriteLine($"Button_Click: {(e.Source as Button).Content}");
58
- //e.Handled = true; // 処理済みにすると↓に来なくなるので注意
59
- }
60
-
61
- private void Window_Button_Click(object sender, RoutedEventArgs e)
62
- {
63
- Debug.WriteLine($"Window_Button_Click: {(e.Source as Button).Content}");
64
- }
65
- }
66
- }
1
+ `MainWrapPanel`の子だけを検知できればいいのなら、`WrapPanel`を継承し`OnVisualChildrenChanged`をオーバーライドすればいいでしょう(試したことのリンク記事はそういう意味です)
2
+
3
+ ですがやりたいことって恐らくそういう意味じゃないんですよね?
4
+ どこに置いたボタン(等)でもイベントを拾いたいって意味ですよね。
5
+
6
+ WPFはルーティングイベントという大変便利な仕組みがあります。
7
+ `MainWindow`で一括して`Button.Click`を拾うことができます。
8
+ [ルーティング イベントの概要 - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/routed-events-overview)
9
+
10
+ イベントによっては(例えば`Button.MouseDown`)処理済みになって来ないので、トンネルイベント(`Previewなんちゃら`)を使用してください。
11
+
12
+ ```xml
13
+ <Window
14
+ x:Class="Questions342011.MainWindow"
15
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
16
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
17
+ Width="800"
18
+ Height="450"
19
+ Button.Click="Window_Button_Click">
20
+ <WrapPanel Name="MainWrapPanel">
21
+ <Button
22
+ Width="75"
23
+ Margin="10"
24
+ Click="AddButton_Click"
25
+ Content="Add" />
26
+ </WrapPanel>
27
+ </Window>
28
+ ```
29
+
30
+ ```cs
31
+ using System.Diagnostics;
32
+ using System.Windows;
33
+ using System.Windows.Controls;
34
+
35
+ namespace Questions342011
36
+ {
37
+ public partial class MainWindow : Window
38
+ {
39
+ public MainWindow() => InitializeComponent();
40
+
41
+ private int count;
42
+ private void AddButton_Click(object sender, RoutedEventArgs e)
43
+ {
44
+ var button = new Button()
45
+ {
46
+ Content = $"Button{++count}",
47
+ Width = 75,
48
+ Margin = new Thickness(10),
49
+ };
50
+ button.Click += Button_Click;
51
+ MainWrapPanel.Children.Add(button);
52
+ }
53
+
54
+ private void Button_Click(object sender, RoutedEventArgs e)
55
+ {
56
+ Debug.WriteLine($"Button_Click: {(e.Source as Button).Content}");
57
+ //e.Handled = true; // 処理済みにすると↓に来なくなるので注意
58
+ }
59
+
60
+ private void Window_Button_Click(object sender, RoutedEventArgs e)
61
+ {
62
+ Debug.WriteLine($"Window_Button_Click: {(e.Source as Button).Content}");
63
+ }
64
+ }
65
+ }
67
66
  ```