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

回答編集履歴

3

見直しキャンペーン中

2023/08/09 09:18

投稿

TN8001
TN8001

スコア10112

answer CHANGED
@@ -1,11 +1,12 @@
1
- FindNameは配下(子供方向)の要素しか探せません。
1
+ `FindName`は配下(子供方向)の要素しか探せません。
2
2
  提示のxamlであれば`var stackPanelPageTop = (StackPanel)Parent;`で取れますね。
3
3
 
4
4
  ---
5
5
  できれば子供から親を操作するのは避けたほうが良いと思います。
6
6
  (子供と親が不可分でもっといろいろな操作がある場合、その限りではありません)
7
- 今回の場合は、
8
7
 
8
+ 今回の場合は、↓とすれば子供は親を気にする必要はなくなります。
9
+
9
10
  ```xml:AllViewWindow.xaml
10
11
  <UserControl
11
12
  x:Class="AppWPF.UserControlObjects.Page.AllViewWindow"
@@ -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

スコア10112

answer CHANGED
@@ -1,88 +1,88 @@
1
- FindNameは配下(子供方向)の要素しか探せません。
2
- 提示のxamlであれば`var stackPanelPageTop = (StackPanel)Parent;`で取れますね。
3
-
4
- ---
5
- できれば子供から親を操作するのは避けたほうが良いと思います。
6
- (子供と親が不可分でもっといろいろな操作がある場合、その限りではありません)
7
- 今回の場合は、
8
- AllViewWindow.xaml
9
- ```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
- PageTop.xaml.cs
21
- ```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
- とすれば子供は親を気にする必要はなくなります。
45
-
46
- ---
47
- 追記 VisualTreeHelper参考コード
48
- PageTop.xaml.cs
49
- ```cs
50
- var stackpanelPage01 = Window.GetWindow(this).FindChild<StackPanel>("StackpanelPage01");
51
- ```
52
- DependencyObjectExtensions.cs
53
- ```cs
54
- using System.Windows;
55
- using System.Windows.Media;
56
-
57
- namespace AppWPF
58
- {
59
- internal static class DependencyObjectExtensions
60
- {
61
- public static T FindChild<T>(this DependencyObject parent, string childName) where T : FrameworkElement
62
- {
63
- if(parent == null) return null;
64
-
65
- var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
66
- for(var i = 0; i < childrenCount; i++)
67
- {
68
- var child = VisualTreeHelper.GetChild(parent, i);
69
- if(child is FrameworkElement frameworkElement && frameworkElement.Name == childName)
70
- {
71
- return (T)frameworkElement;
72
- }
73
- else
74
- {
75
- var foundChild = FindChild<T>(child, childName);
76
- if(foundChild != null)
77
- {
78
- return foundChild;
79
- }
80
- }
81
- }
82
-
83
- return null;
84
- }
85
- }
86
- }
87
- ```
1
+ FindNameは配下(子供方向)の要素しか探せません。
2
+ 提示のxamlであれば`var stackPanelPageTop = (StackPanel)Parent;`で取れますね。
3
+
4
+ ---
5
+ できれば子供から親を操作するのは避けたほうが良いと思います。
6
+ (子供と親が不可分でもっといろいろな操作がある場合、その限りではありません)
7
+ 今回の場合は、
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
+ とすれば子供は親を気にする必要はなくなります。
45
+
46
+ ---
47
+ 追記 VisualTreeHelper参考コード
48
+
49
+ ```cs:PageTop.xaml.cs
50
+ var stackpanelPage01 = Window.GetWindow(this).FindChild<StackPanel>("StackpanelPage01");
51
+ ```
52
+
53
+ ```cs:DependencyObjectExtensions.cs
54
+ using System.Windows;
55
+ using System.Windows.Media;
56
+
57
+ namespace AppWPF
58
+ {
59
+ internal static class DependencyObjectExtensions
60
+ {
61
+ public static T FindChild<T>(this DependencyObject parent, string childName) where T : FrameworkElement
62
+ {
63
+ if(parent == null) return null;
64
+
65
+ var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
66
+ for(var i = 0; i < childrenCount; i++)
67
+ {
68
+ var child = VisualTreeHelper.GetChild(parent, i);
69
+ if(child is FrameworkElement frameworkElement && frameworkElement.Name == childName)
70
+ {
71
+ return (T)frameworkElement;
72
+ }
73
+ else
74
+ {
75
+ var foundChild = FindChild<T>(child, childName);
76
+ if(foundChild != null)
77
+ {
78
+ return foundChild;
79
+ }
80
+ }
81
+ }
82
+
83
+ return null;
84
+ }
85
+ }
86
+ }
87
+ ```
88
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

スコア10112

answer CHANGED
@@ -41,4 +41,48 @@
41
41
  }
42
42
  }
43
43
  ```
44
- とすれば子供は親を気にする必要はなくなります。
44
+ とすれば子供は親を気にする必要はなくなります。
45
+
46
+ ---
47
+ 追記 VisualTreeHelper参考コード
48
+ PageTop.xaml.cs
49
+ ```cs
50
+ var stackpanelPage01 = Window.GetWindow(this).FindChild<StackPanel>("StackpanelPage01");
51
+ ```
52
+ DependencyObjectExtensions.cs
53
+ ```cs
54
+ using System.Windows;
55
+ using System.Windows.Media;
56
+
57
+ namespace AppWPF
58
+ {
59
+ internal static class DependencyObjectExtensions
60
+ {
61
+ public static T FindChild<T>(this DependencyObject parent, string childName) where T : FrameworkElement
62
+ {
63
+ if(parent == null) return null;
64
+
65
+ var childrenCount = VisualTreeHelper.GetChildrenCount(parent);
66
+ for(var i = 0; i < childrenCount; i++)
67
+ {
68
+ var child = VisualTreeHelper.GetChild(parent, i);
69
+ if(child is FrameworkElement frameworkElement && frameworkElement.Name == childName)
70
+ {
71
+ return (T)frameworkElement;
72
+ }
73
+ else
74
+ {
75
+ var foundChild = FindChild<T>(child, childName);
76
+ if(foundChild != null)
77
+ {
78
+ return foundChild;
79
+ }
80
+ }
81
+ }
82
+
83
+ return null;
84
+ }
85
+ }
86
+ }
87
+ ```
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)