回答編集履歴

1

見直しキャンペーン中

2023/07/28 14:54

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,251 +1,126 @@
1
1
  > GridにUserControlを追加する方法
2
2
 
3
-
4
-
5
3
  `Grid`なのは何か理由がありますか?
6
-
7
4
  `Grid`は単に追加していくと重なってしまいますし、MVVMで`Row`・`Column`指定するのは割と面倒です。
8
-
9
-
10
5
 
11
6
  提示C#コードでは`Clear`して`Add`と常に一個しか入らないわけですが、毎回`new`することが重要なんでしょうか?
12
7
 
13
8
 
14
-
15
-
16
-
17
9
  やりたいこと自体はわかりますが、もう少し具体的な例でないと何とも言えません。
18
-
19
-
20
10
 
21
11
  とりあえずありえそうなものを3パターン作ってみました(前2つはViewModelは関係ないし追加もしていませんが^^;
22
12
 
23
-
24
-
25
- ```xaml
13
+ ```xml
26
-
27
14
  <Window
28
-
29
15
  x:Class="Questions352544.Views.MainWindow"
30
-
31
16
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
32
-
33
17
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
34
-
35
18
  xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
36
-
37
19
  xmlns:v="clr-namespace:Questions352544.Views"
38
-
39
20
  xmlns:vm="clr-namespace:Questions352544.ViewModels"
40
-
41
21
  Width="600"
42
-
43
22
  Height="350">
44
-
45
23
  <Window.Resources>
46
-
47
24
  <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
48
-
49
25
  </Window.Resources>
50
26
 
51
-
52
-
53
27
  <Window.DataContext>
54
-
55
28
  <vm:MainWindowViewModel />
56
-
57
29
  </Window.DataContext>
58
30
 
59
-
60
-
61
31
  <Grid>
62
-
63
32
  <Grid.ColumnDefinitions>
64
-
65
33
  <ColumnDefinition />
66
-
67
34
  <ColumnDefinition />
68
-
69
35
  <ColumnDefinition />
70
-
71
36
  </Grid.ColumnDefinitions>
72
37
 
73
-
74
-
75
38
  <GroupBox Header="一回限り出現">
76
-
77
39
  <DockPanel>
78
-
79
40
  <Button Content="Show" DockPanel.Dock="Top">
80
-
81
41
  <behaviors:Interaction.Triggers>
82
-
83
42
  <behaviors:EventTrigger EventName="Click">
84
-
85
43
  <behaviors:ChangePropertyAction
86
-
87
44
  PropertyName="Visibility"
88
-
89
45
  TargetName="userControl1"
90
-
91
46
  Value="Visible" />
92
-
93
47
  </behaviors:EventTrigger>
94
-
95
48
  </behaviors:Interaction.Triggers>
96
-
97
49
  </Button>
98
-
99
50
  <Grid>
100
-
101
51
  <v:UserControl1
102
-
103
52
  x:Name="userControl1"
104
-
105
53
  DataContext="only one"
106
-
107
54
  Visibility="Hidden" />
108
-
109
55
  </Grid>
110
-
111
56
  </DockPanel>
112
-
113
57
  </GroupBox>
114
58
 
115
-
116
-
117
59
  <GroupBox Grid.Column="1" Header="出したり消したり">
118
-
119
60
  <DockPanel>
120
-
121
61
  <ToggleButton
122
-
123
62
  x:Name="toggleButton"
124
-
125
63
  Content="Show&amp;Hide"
126
-
127
64
  DockPanel.Dock="Top" />
128
-
129
65
  <Grid>
130
-
131
66
  <v:UserControl1 DataContext="only one" Visibility="{Binding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=toggleButton}" />
132
-
133
67
  </Grid>
134
-
135
68
  </DockPanel>
136
-
137
69
  </GroupBox>
138
70
 
139
-
140
-
141
71
  <GroupBox Grid.Column="2" Header="どんどん追加">
142
-
143
72
  <DockPanel>
144
-
145
73
  <Button
146
-
147
74
  Command="{Binding AddCommand}"
148
-
149
75
  Content="Add"
150
-
151
76
  DockPanel.Dock="Top" />
152
-
153
77
  <ItemsControl ItemsSource="{Binding Items}">
154
-
155
78
  <ItemsControl.ItemTemplate>
156
-
157
79
  <DataTemplate>
158
-
159
80
  <v:UserControl1 />
160
-
161
81
  </DataTemplate>
162
-
163
82
  </ItemsControl.ItemTemplate>
164
-
165
83
  </ItemsControl>
166
-
167
84
  </DockPanel>
168
-
169
85
  </GroupBox>
170
-
171
86
  </Grid>
172
-
173
87
  </Window>
174
-
175
88
  ```
176
89
 
177
-
178
-
179
- ```xaml
90
+ ```xml
180
-
181
91
  <UserControl
182
-
183
92
  x:Class="Questions352544.Views.UserControl1"
184
-
185
93
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
186
-
187
94
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
188
-
189
95
  <Grid>
190
-
191
96
  <TextBlock Text="{Binding ., StringFormat=UserControl1 - {0}}" />
192
-
193
97
  </Grid>
194
-
195
98
  </UserControl>
196
-
197
99
  ```
198
100
 
199
-
200
-
201
- ```C#
101
+ ```cs
202
-
203
102
  using Livet;
204
-
205
103
  using Livet.Commands;
206
-
207
104
  using System.Collections.ObjectModel;
208
105
 
209
-
210
-
211
106
  namespace Questions352544.ViewModels
212
-
213
107
  {
214
-
215
108
  public class MainWindowViewModel : ViewModel
216
-
217
109
  {
218
-
219
110
  public ViewModelCommand AddCommand { get; }
220
-
221
111
  public ObservableCollection<int> Items { get; } = new ObservableCollection<int>();
222
-
223
-
224
112
 
225
113
  private int count;
226
114
 
227
-
228
-
229
115
  public MainWindowViewModel()
230
-
231
116
  {
232
-
233
117
  AddCommand = new ViewModelCommand(() => Items.Add(++count));
234
-
235
118
  }
236
-
237
119
  }
238
-
239
120
  }
240
-
241
121
  ```
242
122
 
243
-
244
-
245
123
  `ObservableCollection<int>`と、`int`になっている深い意味はありません。
246
-
247
124
  `ObservableCollection<UserControl1ViewModel>`となることのほうが普通かもしれませんが、ケースバイケースです。
248
125
 
249
-
250
-
251
126
  ![アプリ画像](9995beef189399906eb233ddaa2a820e.png)