回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,139 +1,139 @@
|
|
1
|
-
>> [C# - C#、WPF、MVVM形式、で画面に図形を表示したい|teratail](https://teratail.com/questions/349407#reply-478878)
|
2
|
-
>> 例えば↑のようにDataTemplate・classを必要なだけ増やす
|
3
|
-
|
4
|
-
> DataTemplate をクラスごとに出来るんですね。
|
5
|
-
> 確かに、その方法で、プロパティも設定できるなら、必要な分だけ追加するのでも十分そうです。
|
6
|
-
|
7
|
-
提示リンクとほぼ何も変わりませんが、雑に想定コントロール版に書き換えました(不足プロパティがあるでしょうが好きに足してください)
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml
|
17
|
-
xmlns:
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
<
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
<
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
<
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
<
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
<
|
65
|
-
|
66
|
-
|
67
|
-
<Setter Property="Canvas.
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
using System;
|
78
|
-
using System.
|
79
|
-
using System.Windows;
|
80
|
-
using System.Windows.Media;
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
public double
|
88
|
-
public double
|
89
|
-
public double
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
public
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
new
|
131
|
-
new
|
132
|
-
new
|
133
|
-
new
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
1
|
+
>> [C# - C#、WPF、MVVM形式、で画面に図形を表示したい|teratail](https://teratail.com/questions/349407#reply-478878)
|
2
|
+
>> 例えば↑のようにDataTemplate・classを必要なだけ増やす
|
3
|
+
|
4
|
+
> DataTemplate をクラスごとに出来るんですね。
|
5
|
+
> 確かに、その方法で、プロパティも設定できるなら、必要な分だけ追加するのでも十分そうです。
|
6
|
+
|
7
|
+
提示リンクとほぼ何も変わりませんが、雑に想定コントロール版に書き換えました(不足プロパティがあるでしょうが好きに足してください)
|
8
|
+
面倒なので増減はしませんが、`Items`を増減すれば表示も追従します。
|
9
|
+
|
10
|
+
PDFリーダーにこの(`ItemsControl`にバインドするような)方法が向いているかは、ちょっとわからないです。
|
11
|
+
|
12
|
+
```xml
|
13
|
+
<Window
|
14
|
+
x:Class="Questions359699.MainWindow"
|
15
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
16
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
17
|
+
xmlns:local="clr-namespace:Questions359699"
|
18
|
+
Width="800"
|
19
|
+
Height="450">
|
20
|
+
<Window.Resources>
|
21
|
+
<DataTemplate DataType="{x:Type local:PathItem}">
|
22
|
+
<Path
|
23
|
+
Width="{Binding Width}"
|
24
|
+
Height="{Binding Height}"
|
25
|
+
Data="{Binding Data}"
|
26
|
+
Stroke="{Binding Stroke}" />
|
27
|
+
</DataTemplate>
|
28
|
+
<DataTemplate DataType="{x:Type local:EllipseItem}">
|
29
|
+
<Ellipse
|
30
|
+
Width="{Binding Width}"
|
31
|
+
Height="{Binding Height}"
|
32
|
+
Fill="{Binding Fill}" />
|
33
|
+
</DataTemplate>
|
34
|
+
<DataTemplate DataType="{x:Type local:ImageItem}">
|
35
|
+
<Image
|
36
|
+
Width="{Binding Width}"
|
37
|
+
Height="{Binding Height}"
|
38
|
+
Source="{Binding Source}" />
|
39
|
+
</DataTemplate>
|
40
|
+
<DataTemplate DataType="{x:Type local:RectangleItem}">
|
41
|
+
<Rectangle
|
42
|
+
Width="{Binding Width}"
|
43
|
+
Height="{Binding Height}"
|
44
|
+
Fill="{Binding Fill}" />
|
45
|
+
</DataTemplate>
|
46
|
+
<DataTemplate DataType="{x:Type local:RichTextBoxItem}">
|
47
|
+
<RichTextBox Width="{Binding Width}" Height="{Binding Height}">
|
48
|
+
<FlowDocument>
|
49
|
+
<Paragraph>
|
50
|
+
<Run Text="{Binding Text}" />
|
51
|
+
</Paragraph>
|
52
|
+
</FlowDocument>
|
53
|
+
</RichTextBox>
|
54
|
+
</DataTemplate>
|
55
|
+
</Window.Resources>
|
56
|
+
|
57
|
+
<Grid>
|
58
|
+
<ItemsControl ItemsSource="{Binding Items}">
|
59
|
+
<ItemsControl.ItemsPanel>
|
60
|
+
<ItemsPanelTemplate>
|
61
|
+
<Canvas />
|
62
|
+
</ItemsPanelTemplate>
|
63
|
+
</ItemsControl.ItemsPanel>
|
64
|
+
<ItemsControl.ItemContainerStyle>
|
65
|
+
<Style>
|
66
|
+
<Setter Property="Canvas.Left" Value="{Binding X}" />
|
67
|
+
<Setter Property="Canvas.Top" Value="{Binding Y}" />
|
68
|
+
</Style>
|
69
|
+
</ItemsControl.ItemContainerStyle>
|
70
|
+
</ItemsControl>
|
71
|
+
</Grid>
|
72
|
+
</Window>
|
73
|
+
```
|
74
|
+
|
75
|
+
```cs
|
76
|
+
using System;
|
77
|
+
using System.Collections.ObjectModel;
|
78
|
+
using System.Windows;
|
79
|
+
using System.Windows.Media;
|
80
|
+
using System.Windows.Media.Imaging;
|
81
|
+
|
82
|
+
namespace Questions359699
|
83
|
+
{
|
84
|
+
public class Item
|
85
|
+
{
|
86
|
+
public double X { get; set; }
|
87
|
+
public double Y { get; set; }
|
88
|
+
public double Width { get; set; }
|
89
|
+
public double Height { get; set; }
|
90
|
+
}
|
91
|
+
|
92
|
+
public class PathItem : Item
|
93
|
+
{
|
94
|
+
public Brush Stroke { get; set; }
|
95
|
+
public string Data { get; set; }
|
96
|
+
}
|
97
|
+
|
98
|
+
public class EllipseItem : Item
|
99
|
+
{
|
100
|
+
public Brush Fill { get; set; }
|
101
|
+
}
|
102
|
+
|
103
|
+
public class ImageItem : Item
|
104
|
+
{
|
105
|
+
public ImageSource Source { get; set; }
|
106
|
+
}
|
107
|
+
|
108
|
+
public class RectangleItem : Item
|
109
|
+
{
|
110
|
+
public Brush Fill { get; set; }
|
111
|
+
}
|
112
|
+
|
113
|
+
public class RichTextBoxItem : Item
|
114
|
+
{
|
115
|
+
public string Text { get; set; }
|
116
|
+
}
|
117
|
+
|
118
|
+
public partial class MainWindow : Window
|
119
|
+
{
|
120
|
+
public ObservableCollection<Item> Items { get; }
|
121
|
+
|
122
|
+
public MainWindow()
|
123
|
+
{
|
124
|
+
InitializeComponent();
|
125
|
+
DataContext = this;
|
126
|
+
|
127
|
+
Items = new ObservableCollection<Item>
|
128
|
+
{
|
129
|
+
new PathItem { X = 50, Y = 50, Width = 50, Height = 50, Stroke = Brushes.Red, Data = "M0,25L25,50L50,25L25,0Z", },
|
130
|
+
new EllipseItem { X = 200, Y = 50, Width = 50, Height = 100, Fill = Brushes.Green, },
|
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")), },
|
132
|
+
new RectangleItem { X = 200, Y = 200, Width = 100, Height = 50, Fill = Brushes.Blue, },
|
133
|
+
new RichTextBoxItem { X = 400, Y = 50, Width = 300, Height = 300, Text = "RichTextBoxItem" },
|
134
|
+
};
|
135
|
+
}
|
136
|
+
}
|
137
|
+
}
|
138
|
+
```
|
139
|
+

|