回答編集履歴

2

見直しキャンペーン中

2023/07/29 08:36

投稿

TN8001
TN8001

スコア10022

test CHANGED
@@ -1,301 +1,151 @@
1
1
  解決されたようですが、第三者目線ではさっぱり意味が分かんないですね(私の頭が悪いだけかもしれませんが^^;
2
-
3
-
4
2
 
5
3
  ---
6
4
 
7
-
8
-
9
5
  そもそも`HitTest`はどういう意味で使っていますか?
10
-
11
6
  [UIElement.InputHitTest(Point) メソッド (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.uielement.inputhittest?view=net-5.0)
12
-
13
7
  や、
14
-
15
8
  [VisualTreeHelper.HitTest メソッド (System.Windows.Media) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.media.visualtreehelper.hittest?view=net-5.0)
16
-
17
9
  のことですか?
18
-
19
10
  それともマウスクリック等のことですか?
20
11
 
21
-
22
-
23
12
  > Canvas上のGridは見かけ上の位置しか変更できず
24
-
25
13
  > もともとの位置((x,y)=(0,0))でしか、HitTestを行えません。
26
-
27
-
28
14
 
29
15
  そんなことないと思いますけど、どう確認しましたか?
30
16
 
31
-
32
-
33
17
  > rowとcolumnの情報を取得するにはどうしたらよいでしょうか!?
34
18
 
35
-
36
-
37
- 「HitTestでGridを選択したい」(取得したい?)と矛盾するというか、`Grid`の**中に**入っている何かの`HitTest`から、`Row`・`Column`を取得ならわかります。
19
+ `HitTest``Grid`を選択したい」(取得したい?)と矛盾するというか、`Grid`の**中に**入っている何かの`HitTest`から、`Row`・`Column`を取得ならわかります。
38
-
39
- しかし`Grid`自体を取ったところで、その位置の`Row`・`Column`を手軽に出す方法はないと思うのですが(計算することはできるでしょうけど)
20
+ しかし`Grid`自体を取ったところで、その位置の`Row`・`Column`を手軽に出す方法はないと思うのですが(計算することはできるでしょうけど)
40
-
41
-
42
21
 
43
22
  > //無効(LayoutTransformのMSDより)
44
23
 
45
-
46
-
47
24
  これのことですね。
48
-
49
25
  > However, LayoutTransform ignores TranslateTransform operations.
50
-
51
-
52
26
 
53
27
  [FrameworkElement.LayoutTransform Property (System.Windows) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.layouttransform#remarks)
54
28
 
55
-
56
-
57
29
  ---
58
30
 
59
-
60
-
61
31
  なにか質問に書かれていない、前提条件があるのでしょうか?
62
-
63
32
  (コンパイル可能な)簡単なサンプルを提示いただくと、他人にとっても役に立つ質問になるかもしれません。
64
33
 
65
-
66
-
67
- ```xaml
34
+ ```xml
68
-
69
35
  <Window
70
-
71
36
  x:Class="Questions369551.MainWindow"
72
-
73
37
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
74
-
75
38
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
76
-
77
39
  Width="500"
78
-
79
40
  Height="600">
80
-
81
41
  <Grid>
82
-
83
42
  <Grid.RowDefinitions>
84
-
85
43
  <RowDefinition Height="2*" />
86
-
87
44
  <RowDefinition Height="3*" />
88
-
89
45
  </Grid.RowDefinitions>
90
-
91
46
  <GroupBox Header="Canvas in Grid 移動テスト">
92
-
93
47
  <Canvas
94
-
95
48
  x:Name="canvas1"
96
-
97
49
  Background="White"
98
-
99
50
  MouseLeftButtonDown="Canvas_MouseLeftButtonDown">
100
-
101
51
  <Grid
102
-
103
52
  x:Name="grid1"
104
-
105
53
  Width="100"
106
-
107
54
  Height="100"
108
-
109
55
  Background="Blue" />
110
-
111
56
  <Grid
112
-
113
57
  x:Name="grid2"
114
-
115
58
  Width="100"
116
-
117
59
  Height="100"
118
-
119
60
  Background="Blue">
120
-
121
61
  <Grid.RenderTransform>
122
-
123
62
  <TranslateTransform X="100" Y="100" />
124
-
125
63
  </Grid.RenderTransform>
126
-
127
64
  </Grid>
128
-
129
65
  <Grid
130
-
131
66
  x:Name="grid3"
132
-
133
67
  Width="100"
134
-
135
68
  Height="100"
136
-
137
69
  Margin="200,0,0,0"
138
-
139
70
  Background="Blue" />
140
-
141
71
  <Grid
142
-
143
72
  x:Name="grid4"
144
-
145
73
  Canvas.Left="300"
146
-
147
74
  Canvas.Top="100"
148
-
149
75
  Width="100"
150
-
151
76
  Height="100"
152
-
153
77
  Background="Blue" />
154
-
155
78
  </Canvas>
156
-
157
79
  </GroupBox>
158
80
 
159
-
160
-
161
81
  <GroupBox Grid.Row="1" Header="Shape HitTest テスト">
162
-
163
82
  <Canvas x:Name="canvas2" MouseLeftButtonDown="Canvas_MouseLeftButtonDown">
164
-
165
83
  <Grid
166
-
167
84
  Canvas.Left="30"
168
-
169
85
  Canvas.Top="30"
170
-
171
86
  Width="200"
172
-
173
87
  Height="200"
174
-
175
88
  Margin="20,20,0,0"
176
-
177
89
  ShowGridLines="True">
178
-
179
90
  <Grid.RenderTransform>
180
-
181
91
  <TranslateTransform X="10" Y="10" />
182
-
183
92
  </Grid.RenderTransform>
184
-
185
93
  <Grid.RowDefinitions>
186
-
187
94
  <RowDefinition />
188
-
189
95
  <RowDefinition />
190
-
191
96
  </Grid.RowDefinitions>
192
-
193
97
  <Grid.ColumnDefinitions>
194
-
195
98
  <ColumnDefinition />
196
-
197
99
  <ColumnDefinition />
198
-
199
100
  </Grid.ColumnDefinitions>
200
-
201
101
  <Rectangle
202
-
203
102
  x:Name="rectangle"
204
-
205
103
  Grid.Column="1"
206
-
207
104
  Fill="Green" />
208
-
209
105
  <Ellipse
210
-
211
106
  x:Name="ellipse"
212
-
213
107
  Grid.Row="1"
214
-
215
108
  Fill="HotPink" />
216
-
217
109
  <Polygon
218
-
219
110
  x:Name="polygon"
220
-
221
111
  Grid.Row="1"
222
-
223
112
  Grid.Column="1"
224
-
225
113
  Fill="Orange"
226
-
227
114
  Points="0,100 100,100 50,0" />
228
-
229
115
  </Grid>
230
-
231
116
  </Canvas>
232
-
233
117
  </GroupBox>
234
-
235
118
  </Grid>
236
-
237
119
  </Window>
238
-
239
120
  ```
240
121
 
241
-
242
-
243
- ```C#
122
+ ```cs
244
-
245
123
  using System;
246
-
247
124
  using System.Diagnostics;
248
-
249
125
  using System.Windows;
250
-
251
126
  using System.Windows.Controls;
252
-
253
127
  using System.Windows.Input;
254
-
255
128
  using System.Windows.Media;
256
129
 
257
-
258
-
259
130
  namespace Questions369551
260
-
261
131
  {
262
-
263
132
  public partial class MainWindow : Window
264
-
265
133
  {
266
-
267
134
  public MainWindow() => InitializeComponent();
268
135
 
269
-
270
-
271
136
  private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
272
-
273
137
  {
274
-
275
138
  if (sender is Canvas canvas)
276
-
277
139
  {
278
-
279
140
  var p = e.GetPosition(canvas);
280
-
281
141
  if (VisualTreeHelper.HitTest(canvas, p)?.VisualHit is FrameworkElement element)
282
-
283
142
  {
284
-
285
143
  var r = Grid.GetRow(element);
286
-
287
144
  var c = Grid.GetColumn(element);
288
-
289
145
  Debug.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {element.Name} Row:{r} Column:{c}");
290
-
291
146
  }
292
-
293
147
  }
294
-
295
148
  }
296
-
297
149
  }
298
-
299
150
  }
300
-
301
151
  ```

1

誤字

2021/11/16 10:31

投稿

TN8001
TN8001

スコア10022

test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  のことですか?
18
18
 
19
- それもマウスクリック等のことですか?
19
+ それもマウスクリック等のことですか?
20
20
 
21
21
 
22
22