回答編集履歴

1

見直しキャンペーン中

2023/07/29 05:35

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,277 +1,139 @@
1
1
  >> [C# - C#、WPF、MVVM形式、で画面に図形を表示したい|teratail](https://teratail.com/questions/349407#reply-478878)
2
-
3
2
  >> 例えば↑のようにDataTemplate・classを必要なだけ増やす
4
3
 
5
-
6
-
7
4
  > DataTemplate をクラスごとに出来るんですね。
8
-
9
5
  > 確かに、その方法で、プロパティも設定できるなら、必要な分だけ追加するのでも十分そうです。
10
6
 
7
+ 提示リンクとほぼ何も変わりませんが、雑に想定コントロール版に書き換えました(不足プロパティがあるでしょうが好きに足してください)
8
+ 面倒なので増減はしませんが、`Items`を増減すれば表示も追従します。
11
9
 
10
+ PDFリーダーにこの(`ItemsControl`にバインドするような)方法が向いているかは、ちょっとわからないです。
12
11
 
13
- 提示リンクとほぼ何も変わりませんが、雑に想定コントロール版に書き換えました(不足プロパティがあるでしょうが好きに足してください)
14
-
15
-
16
-
17
- 面倒なので増減はしませんが、Itemsを増減すれば表示も追従します。
18
-
19
-
20
-
21
- PDFリーダーにこの(ItemsControlにバインドするような)方法が向いているかは、ちょっとわからないです。
22
-
23
-
24
-
25
- ```xaml
12
+ ```xml
26
-
27
13
  <Window
28
-
29
14
  x:Class="Questions359699.MainWindow"
30
-
31
15
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
32
-
33
16
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
34
-
35
17
  xmlns:local="clr-namespace:Questions359699"
36
-
37
18
  Width="800"
38
-
39
19
  Height="450">
40
-
41
20
  <Window.Resources>
42
-
43
21
  <DataTemplate DataType="{x:Type local:PathItem}">
44
-
45
22
  <Path
46
-
47
23
  Width="{Binding Width}"
48
-
49
24
  Height="{Binding Height}"
50
-
51
25
  Data="{Binding Data}"
52
-
53
26
  Stroke="{Binding Stroke}" />
54
-
55
27
  </DataTemplate>
56
-
57
28
  <DataTemplate DataType="{x:Type local:EllipseItem}">
58
-
59
29
  <Ellipse
60
-
61
30
  Width="{Binding Width}"
62
-
63
31
  Height="{Binding Height}"
64
-
65
32
  Fill="{Binding Fill}" />
66
-
67
33
  </DataTemplate>
68
-
69
34
  <DataTemplate DataType="{x:Type local:ImageItem}">
70
-
71
35
  <Image
72
-
73
36
  Width="{Binding Width}"
74
-
75
37
  Height="{Binding Height}"
76
-
77
38
  Source="{Binding Source}" />
78
-
79
39
  </DataTemplate>
80
-
81
40
  <DataTemplate DataType="{x:Type local:RectangleItem}">
82
-
83
41
  <Rectangle
84
-
85
42
  Width="{Binding Width}"
86
-
87
43
  Height="{Binding Height}"
88
-
89
44
  Fill="{Binding Fill}" />
90
-
91
45
  </DataTemplate>
92
-
93
46
  <DataTemplate DataType="{x:Type local:RichTextBoxItem}">
94
-
95
47
  <RichTextBox Width="{Binding Width}" Height="{Binding Height}">
96
-
97
48
  <FlowDocument>
98
-
99
49
  <Paragraph>
100
-
101
50
  <Run Text="{Binding Text}" />
102
-
103
51
  </Paragraph>
104
-
105
52
  </FlowDocument>
106
-
107
53
  </RichTextBox>
108
-
109
54
  </DataTemplate>
110
-
111
55
  </Window.Resources>
112
56
 
113
-
114
-
115
57
  <Grid>
116
-
117
58
  <ItemsControl ItemsSource="{Binding Items}">
118
-
119
59
  <ItemsControl.ItemsPanel>
120
-
121
60
  <ItemsPanelTemplate>
122
-
123
61
  <Canvas />
124
-
125
62
  </ItemsPanelTemplate>
126
-
127
63
  </ItemsControl.ItemsPanel>
128
-
129
64
  <ItemsControl.ItemContainerStyle>
130
-
131
65
  <Style>
132
-
133
66
  <Setter Property="Canvas.Left" Value="{Binding X}" />
134
-
135
67
  <Setter Property="Canvas.Top" Value="{Binding Y}" />
136
-
137
68
  </Style>
138
-
139
69
  </ItemsControl.ItemContainerStyle>
140
-
141
70
  </ItemsControl>
142
-
143
71
  </Grid>
144
-
145
72
  </Window>
146
-
147
73
  ```
148
74
 
149
-
150
-
151
- ```C#
75
+ ```cs
152
-
153
76
  using System;
154
-
155
77
  using System.Collections.ObjectModel;
156
-
157
78
  using System.Windows;
158
-
159
79
  using System.Windows.Media;
160
-
161
80
  using System.Windows.Media.Imaging;
162
81
 
163
-
164
-
165
82
  namespace Questions359699
166
-
167
83
  {
168
-
169
84
  public class Item
170
-
171
85
  {
172
-
173
86
  public double X { get; set; }
174
-
175
87
  public double Y { get; set; }
176
-
177
88
  public double Width { get; set; }
178
-
179
89
  public double Height { get; set; }
180
-
181
90
  }
182
91
 
183
-
184
-
185
92
  public class PathItem : Item
186
-
187
93
  {
188
-
189
94
  public Brush Stroke { get; set; }
190
-
191
95
  public string Data { get; set; }
192
-
193
96
  }
194
97
 
195
-
196
-
197
98
  public class EllipseItem : Item
198
-
199
99
  {
200
-
201
100
  public Brush Fill { get; set; }
202
-
203
101
  }
204
102
 
205
-
206
-
207
103
  public class ImageItem : Item
208
-
209
104
  {
210
-
211
105
  public ImageSource Source { get; set; }
212
-
213
106
  }
214
107
 
215
-
216
-
217
108
  public class RectangleItem : Item
218
-
219
109
  {
220
-
221
110
  public Brush Fill { get; set; }
222
-
223
111
  }
224
112
 
225
-
226
-
227
113
  public class RichTextBoxItem : Item
228
-
229
114
  {
230
-
231
115
  public string Text { get; set; }
232
-
233
116
  }
234
117
 
235
-
236
-
237
118
  public partial class MainWindow : Window
238
-
239
119
  {
240
-
241
120
  public ObservableCollection<Item> Items { get; }
242
121
 
243
-
244
-
245
122
  public MainWindow()
246
-
247
123
  {
248
-
249
124
  InitializeComponent();
250
-
251
125
  DataContext = this;
252
126
 
253
-
254
-
255
127
  Items = new ObservableCollection<Item>
256
-
257
128
  {
258
-
259
129
  new PathItem { X = 50, Y = 50, Width = 50, Height = 50, Stroke = Brushes.Red, Data = "M0,25L25,50L50,25L25,0Z", },
260
-
261
130
  new EllipseItem { X = 200, Y = 50, Width = 50, Height = 100, Fill = Brushes.Green, },
262
-
263
131
  new ImageItem { X = 50, Y = 200, Width = 32, Height = 32, Source = new BitmapImage(new Uri("https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg")), },
264
-
265
132
  new RectangleItem { X = 200, Y = 200, Width = 100, Height = 50, Fill = Brushes.Blue, },
266
-
267
133
  new RichTextBoxItem { X = 400, Y = 50, Width = 300, Height = 300, Text = "RichTextBoxItem" },
268
-
269
134
  };
270
-
271
135
  }
272
-
273
136
  }
274
-
275
137
  }
276
-
277
138
  ```
139
+ ![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-29/e9ec19fc-7fc1-4f7f-891f-ef64a4855fb4.png)