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

回答編集履歴

1

見直しキャンペーン中

2023/07/29 05:35

投稿

TN8001
TN8001

スコア10108

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
- 面倒なので増減はしませんが、Itemsを増減すれば表示も追従します。
10
-
11
- PDFリーダーにこの(ItemsControlにバインドするような)方法が向いているかは、ちょっとわからないです。
12
-
13
- ```xaml
14
- <Window
15
- x:Class="Questions359699.MainWindow"
16
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
17
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18
- xmlns:local="clr-namespace:Questions359699"
19
- Width="800"
20
- Height="450">
21
- <Window.Resources>
22
- <DataTemplate DataType="{x:Type local:PathItem}">
23
- <Path
24
- Width="{Binding Width}"
25
- Height="{Binding Height}"
26
- Data="{Binding Data}"
27
- Stroke="{Binding Stroke}" />
28
- </DataTemplate>
29
- <DataTemplate DataType="{x:Type local:EllipseItem}">
30
- <Ellipse
31
- Width="{Binding Width}"
32
- Height="{Binding Height}"
33
- Fill="{Binding Fill}" />
34
- </DataTemplate>
35
- <DataTemplate DataType="{x:Type local:ImageItem}">
36
- <Image
37
- Width="{Binding Width}"
38
- Height="{Binding Height}"
39
- Source="{Binding Source}" />
40
- </DataTemplate>
41
- <DataTemplate DataType="{x:Type local:RectangleItem}">
42
- <Rectangle
43
- Width="{Binding Width}"
44
- Height="{Binding Height}"
45
- Fill="{Binding Fill}" />
46
- </DataTemplate>
47
- <DataTemplate DataType="{x:Type local:RichTextBoxItem}">
48
- <RichTextBox Width="{Binding Width}" Height="{Binding Height}">
49
- <FlowDocument>
50
- <Paragraph>
51
- <Run Text="{Binding Text}" />
52
- </Paragraph>
53
- </FlowDocument>
54
- </RichTextBox>
55
- </DataTemplate>
56
- </Window.Resources>
57
-
58
- <Grid>
59
- <ItemsControl ItemsSource="{Binding Items}">
60
- <ItemsControl.ItemsPanel>
61
- <ItemsPanelTemplate>
62
- <Canvas />
63
- </ItemsPanelTemplate>
64
- </ItemsControl.ItemsPanel>
65
- <ItemsControl.ItemContainerStyle>
66
- <Style>
67
- <Setter Property="Canvas.Left" Value="{Binding X}" />
68
- <Setter Property="Canvas.Top" Value="{Binding Y}" />
69
- </Style>
70
- </ItemsControl.ItemContainerStyle>
71
- </ItemsControl>
72
- </Grid>
73
- </Window>
74
- ```
75
-
76
- ```C#
77
- using System;
78
- using System.Collections.ObjectModel;
79
- using System.Windows;
80
- using System.Windows.Media;
81
- using System.Windows.Media.Imaging;
82
-
83
- namespace Questions359699
84
- {
85
- public class Item
86
- {
87
- public double X { get; set; }
88
- public double Y { get; set; }
89
- public double Width { get; set; }
90
- public double Height { get; set; }
91
- }
92
-
93
- public class PathItem : Item
94
- {
95
- public Brush Stroke { get; set; }
96
- public string Data { get; set; }
97
- }
98
-
99
- public class EllipseItem : Item
100
- {
101
- public Brush Fill { get; set; }
102
- }
103
-
104
- public class ImageItem : Item
105
- {
106
- public ImageSource Source { get; set; }
107
- }
108
-
109
- public class RectangleItem : Item
110
- {
111
- public Brush Fill { get; set; }
112
- }
113
-
114
- public class RichTextBoxItem : Item
115
- {
116
- public string Text { get; set; }
117
- }
118
-
119
- public partial class MainWindow : Window
120
- {
121
- public ObservableCollection<Item> Items { get; }
122
-
123
- public MainWindow()
124
- {
125
- InitializeComponent();
126
- DataContext = this;
127
-
128
- Items = new ObservableCollection<Item>
129
- {
130
- new PathItem { X = 50, Y = 50, Width = 50, Height = 50, Stroke = Brushes.Red, Data = "M0,25L25,50L50,25L25,0Z", },
131
- new EllipseItem { X = 200, Y = 50, Width = 50, Height = 100, Fill = Brushes.Green, },
132
- 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")), },
133
- new RectangleItem { X = 200, Y = 200, Width = 100, Height = 50, Fill = Brushes.Blue, },
134
- new RichTextBoxItem { X = 400, Y = 50, Width = 300, Height = 300, Text = "RichTextBoxItem" },
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
+ ![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-29/e9ec19fc-7fc1-4f7f-891f-ef64a4855fb4.png)