回答編集履歴
2
見直しキャンペーン中
test
CHANGED
@@ -1,259 +1,130 @@
|
|
1
1
|
> マウスを右クリックでメニューを表示しています。そのイメージを回転や画像の差し替えなど操作したいため画像の上でクリックした img の名前などを取得したいのですが取得方法がわかりません。
|
2
2
|
|
3
|
-
|
4
|
-
|
5
3
|
クリックは`e.OriginalSource`に発生元が入っています。
|
6
|
-
|
7
4
|
[RoutedEventArgs.OriginalSource プロパティ (System.Windows) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.routedeventargs.originalsource)
|
8
5
|
|
9
|
-
|
10
|
-
|
11
6
|
コンテキストメニューは`PlacementTarget`でとれるんですが、今は`Canvas`についています。
|
12
|
-
|
13
7
|
ここから`Image`をとるのはなかなか厳しいので、単純に`Image`に(も)コンテキストメニューをつけるのが楽そうです。
|
14
|
-
|
15
8
|
[ContextMenu.PlacementTarget プロパティ (System.Windows.Controls) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.contextmenu.placementtarget)
|
16
9
|
|
17
|
-
|
18
|
-
|
19
|
-
```x
|
10
|
+
```xml
|
20
|
-
|
21
11
|
<Window
|
22
|
-
|
23
12
|
x:Class="Questions352983.MainWindow"
|
24
|
-
|
25
13
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
26
|
-
|
27
14
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
28
|
-
|
29
15
|
Width="800"
|
30
|
-
|
31
16
|
Height="600">
|
32
|
-
|
33
17
|
<Window.Resources>
|
34
|
-
|
35
18
|
<ContextMenu x:Key="Menu1">
|
36
|
-
|
37
19
|
<MenuItem Click="MenuItem_ImageSelect" Header="画像選択" />
|
38
|
-
|
39
20
|
</ContextMenu>
|
40
|
-
|
41
21
|
</Window.Resources>
|
42
|
-
|
43
22
|
<Grid>
|
44
|
-
|
45
23
|
<Canvas
|
46
|
-
|
47
24
|
Name="canvas1"
|
48
|
-
|
49
25
|
Background="Transparent"
|
50
|
-
|
51
26
|
ContextMenu="{StaticResource Menu1}"
|
52
|
-
|
53
27
|
MouseDown="Canvas1_MouseDown" />
|
54
|
-
|
55
28
|
</Grid>
|
56
|
-
|
57
29
|
</Window>
|
58
|
-
|
59
30
|
```
|
60
31
|
|
61
|
-
|
62
|
-
|
63
|
-
```
|
32
|
+
```cs
|
64
|
-
|
65
33
|
using System;
|
66
|
-
|
67
34
|
using System.Collections.Generic;
|
68
|
-
|
69
35
|
using System.Diagnostics;
|
70
|
-
|
71
36
|
using System.Windows;
|
72
|
-
|
73
37
|
using System.Windows.Controls;
|
74
|
-
|
75
38
|
using System.Windows.Input;
|
76
|
-
|
77
39
|
using System.Windows.Media;
|
78
|
-
|
79
40
|
using System.Windows.Media.Imaging;
|
80
41
|
|
81
|
-
|
82
|
-
|
83
42
|
namespace Questions352983
|
84
|
-
|
85
43
|
{
|
86
|
-
|
87
44
|
public partial class MainWindow : Window
|
88
|
-
|
89
45
|
{
|
90
|
-
|
91
46
|
public MainWindow()
|
92
|
-
|
93
47
|
{
|
94
|
-
|
95
48
|
InitializeComponent();
|
96
49
|
|
97
|
-
|
98
|
-
|
99
50
|
var count = 0;
|
100
|
-
|
101
51
|
var lineThick = 3;
|
102
|
-
|
103
52
|
var menu = (ContextMenu)Resources["Menu1"];
|
104
|
-
|
105
53
|
var items = new List<Item>
|
106
|
-
|
107
54
|
{
|
108
|
-
|
109
55
|
new Item { X = 20, Y = 20, Width = 500, Height = 300, BorderBrush = Brushes.Red, },
|
110
|
-
|
111
56
|
new Item { X = 60, Y = 360, Width = 190, Height = 50, BorderBrush = Brushes.Red, },
|
112
|
-
|
113
57
|
new Item { X = 280, Y = 340, Width = 240, Height = 200, BorderBrush = Brushes.Red, },
|
114
|
-
|
115
58
|
};
|
116
|
-
|
117
59
|
foreach (var item in items)
|
118
|
-
|
119
60
|
{
|
120
|
-
|
121
61
|
var image = new Image
|
122
|
-
|
123
62
|
{
|
124
|
-
|
125
63
|
ContextMenu = menu,
|
126
|
-
|
127
64
|
Name = "img" + ++count,
|
128
|
-
|
129
65
|
Width = item.Width,
|
130
|
-
|
131
66
|
Height = item.Height,
|
132
|
-
|
133
67
|
Source = new BitmapImage(new Uri("https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg")),
|
134
|
-
|
135
68
|
};
|
136
|
-
|
137
69
|
// BorderのContextMenuを動かすため(Imageが上に重なってると動かないので)
|
138
|
-
|
139
70
|
// Borderの中にImageを入れたが本題には無関係
|
140
|
-
|
141
71
|
//Canvas.SetTop(image, item.Y);
|
142
|
-
|
143
72
|
//Canvas.SetLeft(image, item.X);
|
144
73
|
|
145
|
-
|
146
|
-
|
147
74
|
var border = new Border
|
148
|
-
|
149
75
|
{
|
150
|
-
|
151
76
|
ContextMenu = menu,
|
152
|
-
|
153
77
|
Background = Brushes.Transparent,
|
154
|
-
|
155
78
|
BorderBrush = item.BorderBrush,
|
156
|
-
|
157
79
|
BorderThickness = new Thickness(lineThick),
|
158
|
-
|
159
80
|
Width = item.Width,
|
160
|
-
|
161
81
|
Height = item.Height,
|
162
|
-
|
163
82
|
Child = image,
|
164
|
-
|
165
83
|
};
|
166
|
-
|
167
84
|
Canvas.SetTop(border, item.Y);
|
168
|
-
|
169
85
|
Canvas.SetLeft(border, item.X);
|
170
86
|
|
171
|
-
|
172
|
-
|
173
87
|
canvas1.Children.Add(border);
|
174
|
-
|
175
88
|
//canvas1.Children.Add(image);
|
176
|
-
|
177
89
|
}
|
178
|
-
|
179
90
|
}
|
180
91
|
|
181
|
-
|
182
|
-
|
183
92
|
private void MenuItem_ImageSelect(object sender, RoutedEventArgs e)
|
184
|
-
|
185
93
|
{
|
186
|
-
|
187
94
|
if (sender is MenuItem menuItem)
|
188
|
-
|
189
95
|
{
|
190
|
-
|
191
96
|
if (menuItem.Parent is ContextMenu contextMenu)
|
192
|
-
|
193
97
|
{
|
194
|
-
|
195
98
|
// 面倒だったのでMenu1を共用したが、ImageやCanvas専用メニューなら決め打ちでOK
|
196
|
-
|
197
99
|
if (contextMenu.PlacementTarget is Image image)
|
198
|
-
|
199
100
|
{
|
200
|
-
|
201
101
|
Debug.WriteLine($"MenuItem_ImageSelect: {image.Name}");
|
202
|
-
|
203
102
|
}
|
204
|
-
|
205
103
|
if (contextMenu.PlacementTarget is Border)
|
206
|
-
|
207
104
|
{
|
208
|
-
|
209
105
|
Debug.WriteLine($"MenuItem_ImageSelect: Border");
|
210
|
-
|
211
106
|
}
|
212
|
-
|
213
107
|
if (contextMenu.PlacementTarget is Canvas)
|
214
|
-
|
215
108
|
{
|
216
|
-
|
217
109
|
Debug.WriteLine($"MenuItem_ImageSelect: Canvas");
|
218
|
-
|
219
110
|
}
|
220
|
-
|
221
111
|
}
|
222
|
-
|
223
112
|
}
|
224
|
-
|
225
113
|
}
|
226
114
|
|
227
|
-
|
228
|
-
|
229
115
|
private void Canvas1_MouseDown(object sender, MouseButtonEventArgs e)
|
230
|
-
|
231
116
|
{
|
232
|
-
|
233
117
|
Debug.WriteLine($"Canvas1_MouseDown: {e.OriginalSource}");
|
234
|
-
|
235
118
|
}
|
236
|
-
|
237
119
|
}
|
238
120
|
|
239
|
-
|
240
|
-
|
241
121
|
class Item
|
242
|
-
|
243
122
|
{
|
244
|
-
|
245
123
|
public double X { get; set; }
|
246
|
-
|
247
124
|
public double Y { get; set; }
|
248
|
-
|
249
125
|
public double Width { get; set; }
|
250
|
-
|
251
126
|
public double Height { get; set; }
|
252
|
-
|
253
127
|
public Brush BorderBrush { get; set; }
|
254
|
-
|
255
128
|
}
|
256
|
-
|
257
129
|
}
|
258
|
-
|
259
130
|
```
|
1
編集ミス
test
CHANGED
@@ -22,10 +22,6 @@
|
|
22
22
|
|
23
23
|
x:Class="Questions352983.MainWindow"
|
24
24
|
|
25
|
-
x<Window
|
26
|
-
|
27
|
-
x:Class="Questions352983.MainWindow"
|
28
|
-
|
29
25
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
30
26
|
|
31
27
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|