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

回答編集履歴

1

見直しキャンペーン中

2023/07/18 21:23

投稿

TN8001
TN8001

スコア10166

answer CHANGED
@@ -1,102 +1,102 @@
1
- 愚直に書くとだるいですね。
2
- 子孫探索には`LogicalTreeHelper`や`VisualTreeHelper`があります(詳細は調べてください)
3
- あまり使いやすくないので拡張メソッドにしておくと楽でいいです。
4
-
5
- ```xaml
6
- <Window
7
- x:Class="Questions235664.MainWindow"
8
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
9
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
10
- <Window.Resources>
11
- <Viewbox x:Key="testCanvasImage">
12
- <Canvas>
13
- <Canvas>
14
- <Canvas>
15
- <Path Fill="#000000" StrokeThickness="0">
16
- <Path.Data>
17
- <PathGeometry Figures="" />
18
- </Path.Data>
19
- </Path>
20
- <Path x:Name="Path2" Fill="#FFFFFF" />
21
- </Canvas>
22
- </Canvas>
23
- </Canvas>
24
- </Viewbox>
25
- </Window.Resources>
26
- </Window>
27
- ```
28
-
29
-
30
- ```C#
31
- using System;
32
- using System.Collections.Generic;
33
- using System.Diagnostics;
34
- using System.Linq;
35
- using System.Windows;
36
- using System.Windows.Controls;
37
- using System.Windows.Media;
38
- using System.Windows.Shapes;
39
-
40
- namespace Questions235664
41
- {
42
- public partial class MainWindow : Window
43
- {
44
- public MainWindow()
45
- {
46
- InitializeComponent();
47
-
48
- var viewBox = FindResource("testCanvasImage") as Viewbox;
49
-
50
- // 愚直に書くと
51
- var canvas1 = viewBox.Child as Canvas;
52
- var canvas2 = canvas1.Children[0] as Canvas;
53
- var canvas3 = canvas2.Children[0] as Canvas;
54
- var path = canvas3.Children[0] as Path;
55
- Debug.WriteLine(path.Fill);
56
-
57
- // 便利拡張メソッド使用
58
- path = viewBox.Descendants<Path>().First();
59
- Debug.WriteLine(path.Fill);
60
-
61
- // 子孫に探し物が複数ある場合Nameかなんかを付けておく
62
- path = viewBox.Descendants<Path>().First(x => x.Name == "Path2");
63
- Debug.WriteLine(path.Fill);
64
- }
65
- }
66
-
67
- // https://blog.xin9le.net/entry/2013/10/29/222336
68
- static class DependencyObjectExtensions
69
- {
70
- public static IEnumerable<DependencyObject> Children(this DependencyObject obj)
71
- {
72
- if(obj == null) throw new ArgumentNullException(nameof(obj));
73
-
74
- var count = VisualTreeHelper.GetChildrenCount(obj);
75
- if(count == 0) yield break;
76
-
77
- for(var i = 0; i < count; i++)
78
- {
79
- var child = VisualTreeHelper.GetChild(obj, i);
80
- if(child != null) yield return child;
81
- }
82
- }
83
- public static IEnumerable<DependencyObject> Descendants(this DependencyObject obj)
84
- {
85
- if(obj == null) throw new ArgumentNullException(nameof(obj));
86
-
87
- foreach(var child in obj.Children())
88
- {
89
- yield return child;
90
- foreach(var grandChild in child.Descendants())
91
- yield return grandChild;
92
- }
93
- }
94
- public static IEnumerable<T> Children<T>(this DependencyObject obj) where T : DependencyObject
95
- => obj.Children().OfType<T>();
96
- public static IEnumerable<T> Descendants<T>(this DependencyObject obj) where T : DependencyObject
97
- => obj.Descendants().OfType<T>();
98
- }
99
- }
100
- ```
101
-
1
+ 愚直に書くとだるいですね。
2
+ 子孫探索には`LogicalTreeHelper`や`VisualTreeHelper`があります(詳細は調べてください)
3
+ あまり使いやすくないので拡張メソッドにしておくと楽でいいです。
4
+
5
+ ```xml
6
+ <Window
7
+ x:Class="Questions235664.MainWindow"
8
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
9
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
10
+ <Window.Resources>
11
+ <Viewbox x:Key="testCanvasImage">
12
+ <Canvas>
13
+ <Canvas>
14
+ <Canvas>
15
+ <Path Fill="#000000" StrokeThickness="0">
16
+ <Path.Data>
17
+ <PathGeometry Figures="" />
18
+ </Path.Data>
19
+ </Path>
20
+ <Path x:Name="Path2" Fill="#FFFFFF" />
21
+ </Canvas>
22
+ </Canvas>
23
+ </Canvas>
24
+ </Viewbox>
25
+ </Window.Resources>
26
+ </Window>
27
+ ```
28
+
29
+
30
+ ```cs
31
+ using System;
32
+ using System.Collections.Generic;
33
+ using System.Diagnostics;
34
+ using System.Linq;
35
+ using System.Windows;
36
+ using System.Windows.Controls;
37
+ using System.Windows.Media;
38
+ using System.Windows.Shapes;
39
+
40
+ namespace Questions235664
41
+ {
42
+ public partial class MainWindow : Window
43
+ {
44
+ public MainWindow()
45
+ {
46
+ InitializeComponent();
47
+
48
+ var viewBox = FindResource("testCanvasImage") as Viewbox;
49
+
50
+ // 愚直に書くと
51
+ var canvas1 = viewBox.Child as Canvas;
52
+ var canvas2 = canvas1.Children[0] as Canvas;
53
+ var canvas3 = canvas2.Children[0] as Canvas;
54
+ var path = canvas3.Children[0] as Path;
55
+ Debug.WriteLine(path.Fill);
56
+
57
+ // 便利拡張メソッド使用
58
+ path = viewBox.Descendants<Path>().First();
59
+ Debug.WriteLine(path.Fill);
60
+
61
+ // 子孫に探し物が複数ある場合Nameかなんかを付けておく
62
+ path = viewBox.Descendants<Path>().First(x => x.Name == "Path2");
63
+ Debug.WriteLine(path.Fill);
64
+ }
65
+ }
66
+
67
+ // https://blog.xin9le.net/entry/2013/10/29/222336
68
+ static class DependencyObjectExtensions
69
+ {
70
+ public static IEnumerable<DependencyObject> Children(this DependencyObject obj)
71
+ {
72
+ if(obj == null) throw new ArgumentNullException(nameof(obj));
73
+
74
+ var count = VisualTreeHelper.GetChildrenCount(obj);
75
+ if(count == 0) yield break;
76
+
77
+ for(var i = 0; i < count; i++)
78
+ {
79
+ var child = VisualTreeHelper.GetChild(obj, i);
80
+ if(child != null) yield return child;
81
+ }
82
+ }
83
+ public static IEnumerable<DependencyObject> Descendants(this DependencyObject obj)
84
+ {
85
+ if(obj == null) throw new ArgumentNullException(nameof(obj));
86
+
87
+ foreach(var child in obj.Children())
88
+ {
89
+ yield return child;
90
+ foreach(var grandChild in child.Descendants())
91
+ yield return grandChild;
92
+ }
93
+ }
94
+ public static IEnumerable<T> Children<T>(this DependencyObject obj) where T : DependencyObject
95
+ => obj.Children().OfType<T>();
96
+ public static IEnumerable<T> Descendants<T>(this DependencyObject obj) where T : DependencyObject
97
+ => obj.Descendants().OfType<T>();
98
+ }
99
+ }
100
+ ```
101
+
102
102
  参考 [VisualTreeの子孫要素を取得する - xin9le.net](https://blog.xin9le.net/entry/2013/10/29/222336)