回答編集履歴

3

見直しキャンペーン中

2023/08/09 09:18

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,10 +1,11 @@
1
- FindNameは配下(子供方向)の要素しか探せません。
1
+ `FindName`は配下(子供方向)の要素しか探せません。
2
2
  提示のxamlであれば`var stackPanelPageTop = (StackPanel)Parent;`で取れますね。
3
3
 
4
4
  ---
5
5
  できれば子供から親を操作するのは避けたほうが良いと思います。
6
6
  (子供と親が不可分でもっといろいろな操作がある場合、その限りではありません)
7
+
7
- 今回の場合は、
8
+ 今回の場合は、↓とすれば子供は親を気にする必要はなくなります。
8
9
 
9
10
  ```xml:AllViewWindow.xaml
10
11
  <UserControl
@@ -41,10 +42,9 @@
41
42
  }
42
43
  }
43
44
  ```
44
- とすれば子供は親を気にする必要はなくなります。
45
45
 
46
46
  ---
47
- 追記 VisualTreeHelper参考コード
47
+ 追記 `VisualTreeHelper`参考コード
48
48
 
49
49
  ```cs:PageTop.xaml.cs
50
50
  var stackpanelPage01 = Window.GetWindow(this).FindChild<StackPanel>("StackpanelPage01");

2

見直しキャンペーン中

2023/07/17 04:41

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,175 +1,88 @@
1
1
  FindNameは配下(子供方向)の要素しか探せません。
2
-
3
2
  提示のxamlであれば`var stackPanelPageTop = (StackPanel)Parent;`で取れますね。
4
3
 
4
+ ---
5
+ できれば子供から親を操作するのは避けたほうが良いと思います。
6
+ (子供と親が不可分でもっといろいろな操作がある場合、その限りではありません)
7
+ 今回の場合は、
5
8
 
9
+ ```xml:AllViewWindow.xaml
10
+ <UserControl
11
+ x:Class="AppWPF.UserControlObjects.Page.AllViewWindow"
12
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
+ xmlns:page="clr-namespace:AppWPF.UserControlObjects.Page">
15
+ <StackPanel Visibility="{Binding PageVisibility, ElementName=PageTop}">
16
+ <page:PageTop x:Name="PageTop" />
17
+ </StackPanel>
18
+ </UserControl>
19
+ ```
20
+
21
+ ```cs:PageTop.xaml.cs
22
+ using System.Windows;
23
+ using System.Windows.Controls;
24
+
25
+ namespace AppWPF.UserControlObjects.Page
26
+ {
27
+ public partial class PageTop : UserControl
28
+ {
29
+ public Visibility PageVisibility
30
+ {
31
+ get => (Visibility)GetValue(PageVisibilityProperty);
32
+ set => SetValue(PageVisibilityProperty, value);
33
+ }
34
+ public static readonly DependencyProperty PageVisibilityProperty =
35
+ DependencyProperty.Register(nameof(PageVisibility), typeof(Visibility), typeof(PageTop),
36
+ new PropertyMetadata(Visibility.Visible));
37
+
38
+ public PageTop() => InitializeComponent();
39
+ private void TestButton_Click(object sender, RoutedEventArgs e)
40
+ => PageVisibility = Visibility.Hidden;
41
+ }
42
+ }
43
+ ```
44
+ とすれば子供は親を気にする必要はなくなります。
6
45
 
7
46
  ---
47
+ 追記 VisualTreeHelper参考コード
8
48
 
9
- できれば子供から親を操作するのは避けたほうが良いと思います。
10
-
11
- (子供と親が不可分でもっといろいろな操作がある場合、その限りではありません)
12
-
13
- 今回の場合は、
14
-
15
- AllViewWindow.xaml
49
+ ```cs:PageTop.xaml.cs
16
-
17
- ```xaml
18
-
19
- <UserControl
20
-
21
- x:Class="AppWPF.UserControlObjects.Page.AllViewWindow"
22
-
23
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
24
-
25
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
26
-
27
- xmlns:page="clr-namespace:AppWPF.UserControlObjects.Page">
28
-
29
- <StackPanel Visibility="{Binding PageVisibility, ElementName=PageTop}">
50
+ var stackpanelPage01 = Window.GetWindow(this).FindChild<StackPanel>("StackpanelPage01");
30
-
31
- <page:PageTop x:Name="PageTop" />
32
-
33
- </StackPanel>
34
-
35
- </UserControl>
36
-
37
51
  ```
38
52
 
39
- PageTop.xaml.cs
53
+ ```cs:DependencyObjectExtensions.cs
40
-
41
- ```CS
42
-
43
54
  using System.Windows;
44
-
45
- using System.Windows.Controls;
46
-
47
-
48
-
49
- namespace AppWPF.UserControlObjects.Page
50
-
51
- {
52
-
53
- public partial class PageTop : UserControl
54
-
55
- {
56
-
57
- public Visibility PageVisibility
58
-
59
- {
60
-
61
- get => (Visibility)GetValue(PageVisibilityProperty);
62
-
63
- set => SetValue(PageVisibilityProperty, value);
64
-
65
- }
66
-
67
- public static readonly DependencyProperty PageVisibilityProperty =
68
-
69
- DependencyProperty.Register(nameof(PageVisibility), typeof(Visibility), typeof(PageTop),
70
-
71
- new PropertyMetadata(Visibility.Visible));
72
-
73
-
74
-
75
- public PageTop() => InitializeComponent();
76
-
77
- private void TestButton_Click(object sender, RoutedEventArgs e)
78
-
79
- => PageVisibility = Visibility.Hidden;
80
-
81
- }
82
-
83
- }
84
-
85
- ```
86
-
87
- とすれば子供は親を気にする必要はなくなります。
88
-
89
-
90
-
91
- ---
92
-
93
- 追記 VisualTreeHelper参考コード
94
-
95
- PageTop.xaml.cs
96
-
97
- ```cs
98
-
99
- var stackpanelPage01 = Window.GetWindow(this).FindChild<StackPanel>("StackpanelPage01");
100
-
101
- ```
102
-
103
- DependencyObjectExtensions.cs
104
-
105
- ```cs
106
-
107
- using System.Windows;
108
-
109
55
  using System.Windows.Media;
110
56
 
111
-
112
-
113
57
  namespace AppWPF
114
-
115
58
  {
116
-
117
59
  internal static class DependencyObjectExtensions
118
-
119
60
  {
120
-
121
61
  public static T FindChild<T>(this DependencyObject parent, string childName) where T : FrameworkElement
122
-
123
62
  {
124
-
125
63
  if(parent == null) return null;
126
64
 
127
-
128
-
129
65
  var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
130
-
131
66
  for(var i = 0; i < childrenCount; i++)
132
-
133
67
  {
134
-
135
68
  var child = VisualTreeHelper.GetChild(parent, i);
136
-
137
69
  if(child is FrameworkElement frameworkElement && frameworkElement.Name == childName)
138
-
139
70
  {
140
-
141
71
  return (T)frameworkElement;
142
-
143
72
  }
144
-
145
73
  else
146
-
147
74
  {
148
-
149
75
  var foundChild = FindChild<T>(child, childName);
150
-
151
76
  if(foundChild != null)
152
-
153
77
  {
154
-
155
78
  return foundChild;
156
-
157
79
  }
158
-
159
80
  }
160
-
161
81
  }
162
82
 
163
-
164
-
165
83
  return null;
166
-
167
84
  }
168
-
169
85
  }
170
-
171
86
  }
172
-
173
87
  ```
174
-
175
88
  [How can I find WPF controls by name or type? - Stack Overflow](https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type/1759923)

1

追記 VisualTreeHelper参考コード

2019/11/22 05:21

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -85,3 +85,91 @@
85
85
  ```
86
86
 
87
87
  とすれば子供は親を気にする必要はなくなります。
88
+
89
+
90
+
91
+ ---
92
+
93
+ 追記 VisualTreeHelper参考コード
94
+
95
+ PageTop.xaml.cs
96
+
97
+ ```cs
98
+
99
+ var stackpanelPage01 = Window.GetWindow(this).FindChild<StackPanel>("StackpanelPage01");
100
+
101
+ ```
102
+
103
+ DependencyObjectExtensions.cs
104
+
105
+ ```cs
106
+
107
+ using System.Windows;
108
+
109
+ using System.Windows.Media;
110
+
111
+
112
+
113
+ namespace AppWPF
114
+
115
+ {
116
+
117
+ internal static class DependencyObjectExtensions
118
+
119
+ {
120
+
121
+ public static T FindChild<T>(this DependencyObject parent, string childName) where T : FrameworkElement
122
+
123
+ {
124
+
125
+ if(parent == null) return null;
126
+
127
+
128
+
129
+ var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
130
+
131
+ for(var i = 0; i < childrenCount; i++)
132
+
133
+ {
134
+
135
+ var child = VisualTreeHelper.GetChild(parent, i);
136
+
137
+ if(child is FrameworkElement frameworkElement && frameworkElement.Name == childName)
138
+
139
+ {
140
+
141
+ return (T)frameworkElement;
142
+
143
+ }
144
+
145
+ else
146
+
147
+ {
148
+
149
+ var foundChild = FindChild<T>(child, childName);
150
+
151
+ if(foundChild != null)
152
+
153
+ {
154
+
155
+ return foundChild;
156
+
157
+ }
158
+
159
+ }
160
+
161
+ }
162
+
163
+
164
+
165
+ return null;
166
+
167
+ }
168
+
169
+ }
170
+
171
+ }
172
+
173
+ ```
174
+
175
+ [How can I find WPF controls by name or type? - Stack Overflow](https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type/1759923)