質問編集履歴

1

ソース追加

2015/10/15 01:42

投稿

pan
pan

スコア12

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,465 @@
9
9
  DataGridに最初新規行が表示されている状態で、その新規行を編集すると新たな新規行を追加したいのですがやり方が分かりません。
10
10
 
11
11
  どなたか分かる方がいらしたら教えて頂きたいです。
12
+
13
+
14
+
15
+ 追加
16
+
17
+ DataGridの機能で新規行を追加するとNewItemPlaceholderになります。Modelに紐づいた新規行を追加したです。
18
+
19
+ ```
20
+
21
+ Public Class Window1ViewModel
22
+
23
+ Public Sub New()
24
+
25
+ Me.DataCollection.Add(New Model() With {
26
+
27
+ .ComboIndex = 2,
28
+
29
+ .No = 1,
30
+
31
+ .Test1 = "1",
32
+
33
+ .Test2 = "11",
34
+
35
+ .Test3 = "111"})
36
+
37
+
38
+
39
+ Me.DataCollection.Add(New Model() With {
40
+
41
+ .ComboIndex = 0,
42
+
43
+ .No = 2,
44
+
45
+ .Test1 = "2",
46
+
47
+ .Test2 = "22",
48
+
49
+ .Test3 = "222"})
50
+
51
+
52
+
53
+ End Sub
54
+
55
+ End Class
56
+
57
+
58
+
59
+ Public Class TestList
60
+
61
+ Inherits List(Of ComboModel)
62
+
63
+
64
+
65
+ Public Sub New()
66
+
67
+ Me.Add(New ComboModel(0, "データ1"))
68
+
69
+ Me.Add(New ComboModel(1, "データ2"))
70
+
71
+ Me.Add(New ComboModel(2, "データ3"))
72
+
73
+ End Sub
74
+
75
+ End Class
76
+
77
+
78
+
79
+ Public Class Model
80
+
81
+ Inherits ViewModelBase
82
+
83
+
84
+
85
+ Private _ComboIndex As String
86
+
87
+ Public Property ComboIndex() As String
88
+
89
+ Get
90
+
91
+ Return _ComboIndex
92
+
93
+ End Get
94
+
95
+ Set(ByVal value As String)
96
+
97
+ _ComboIndex = value
98
+
99
+ End Set
100
+
101
+ End Property
102
+
103
+
104
+
105
+ Private _No As Integer
106
+
107
+ Public Property No() As Integer
108
+
109
+ Get
110
+
111
+ Return _No
112
+
113
+ End Get
114
+
115
+ Set(ByVal value As Integer)
116
+
117
+ _No = value
118
+
119
+
120
+
121
+ OnPropertyChanged("No")
122
+
123
+ End Set
124
+
125
+ End Property
126
+
127
+
128
+
129
+ Private _Test1 As String
130
+
131
+ Public Property Test1() As String
132
+
133
+ Get
134
+
135
+ Return _Test2
136
+
137
+ End Get
138
+
139
+ Set(ByVal value As String)
140
+
141
+ _Test1 = value
142
+
143
+
144
+
145
+ OnPropertyChanged("Test1")
146
+
147
+ End Set
148
+
149
+ End Property
150
+
151
+
152
+
153
+ Private _Test2 As String
154
+
155
+ Public Property Test2() As String
156
+
157
+ Get
158
+
159
+ Return _Test2
160
+
161
+ End Get
162
+
163
+ Set(ByVal value As String)
164
+
165
+ _Test2 = value
166
+
167
+
168
+
169
+ OnPropertyChanged("Test2")
170
+
171
+ End Set
172
+
173
+ End Property
174
+
175
+
176
+
177
+ Private _Test3 As String
178
+
179
+ Public Property Test3() As String
180
+
181
+ Get
182
+
183
+ Return _Test3
184
+
185
+ End Get
186
+
187
+ Set(ByVal value As String)
188
+
189
+ _Test3 = value
190
+
191
+
192
+
193
+ OnPropertyChanged("Test3")
194
+
195
+ End Set
196
+
197
+ End Property
198
+
199
+ End Class
200
+
201
+ ```
202
+
203
+
204
+
205
+ ```
206
+
207
+ <Window x:Class="Window1"
208
+
209
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
210
+
211
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
212
+
213
+ xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
214
+
215
+ xmlns:local="clr-namespace:Sample"
216
+
217
+ Title="Window1" Height="400" Width="650">
218
+
219
+
220
+
221
+ <Window.Resources>
222
+
223
+ <local:TestList x:Key="TestList" />
224
+
225
+ </Window.Resources>
226
+
227
+ <Window.DataContext>
228
+
229
+ <local:Window1ViewModel/>
230
+
231
+ </Window.DataContext>
232
+
233
+
234
+
235
+ <StackPanel Orientation="Vertical">
236
+
237
+ <DataGrid Grid.Row="1" Name="DataGrid1" ItemsSource="{Binding DataCollection}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" AutoGenerateColumns="False" Height="300" SelectionMode="Single" >
238
+
239
+ <DataGrid.Columns>
240
+
241
+ <DataGridTemplateColumn>
242
+
243
+ <DataGridTemplateColumn.Header>
244
+
245
+ <Grid>
246
+
247
+ <Grid.RowDefinitions>
248
+
249
+ <RowDefinition />
250
+
251
+ <RowDefinition />
252
+
253
+ </Grid.RowDefinitions>
254
+
255
+ <Label Grid.Row="0" Grid.RowSpan="2" Content="てすと1" />
256
+
257
+ </Grid>
258
+
259
+ </DataGridTemplateColumn.Header>
260
+
261
+
262
+
263
+ <DataGridTemplateColumn.CellTemplate>
264
+
265
+ <DataTemplate>
266
+
267
+ <Grid>
268
+
269
+ <Grid.RowDefinitions>
270
+
271
+ <RowDefinition />
272
+
273
+ <RowDefinition />
274
+
275
+ </Grid.RowDefinitions>
276
+
277
+ <TextBox Grid.Row="0" Grid.RowSpan="2" Text="{Binding Test1}" />
278
+
279
+ </Grid>
280
+
281
+ </DataTemplate>
282
+
283
+ </DataGridTemplateColumn.CellTemplate>
284
+
285
+ </DataGridTemplateColumn>
286
+
287
+
288
+
289
+ <DataGridTemplateColumn>
290
+
291
+ <DataGridTemplateColumn.Header>
292
+
293
+ <DataGridColumnHeader>
294
+
295
+ <Grid>
296
+
297
+ <Grid.RowDefinitions>
298
+
299
+ <RowDefinition />
300
+
301
+ <RowDefinition />
302
+
303
+ </Grid.RowDefinitions>
304
+
305
+ <Label Grid.Row="0" Content="てすと2" />
306
+
307
+ <Label Grid.Row="1" Content="てすと3" />
308
+
309
+ </Grid>
310
+
311
+ </DataGridColumnHeader>
312
+
313
+ </DataGridTemplateColumn.Header>
314
+
315
+
316
+
317
+ <DataGridTemplateColumn.CellTemplate>
318
+
319
+ <DataTemplate>
320
+
321
+ <Grid>
322
+
323
+ <Grid.RowDefinitions>
324
+
325
+ <RowDefinition />
326
+
327
+ <RowDefinition />
328
+
329
+ </Grid.RowDefinitions>
330
+
331
+ <TextBox Grid.Row="0" Text="{Binding Test2, UpdateSourceTrigger=PropertyChanged}" />
332
+
333
+ <TextBox Grid.Row="1" Text="{Binding Test3, UpdateSourceTrigger=PropertyChanged}" />
334
+
335
+ </Grid>
336
+
337
+ </DataTemplate>
338
+
339
+ </DataGridTemplateColumn.CellTemplate>
340
+
341
+ </DataGridTemplateColumn>
342
+
343
+
344
+
345
+ <DataGridTemplateColumn>
346
+
347
+ <DataGridTemplateColumn.Header>
348
+
349
+ <Grid>
350
+
351
+ <Grid.RowDefinitions>
352
+
353
+ <RowDefinition />
354
+
355
+ <RowDefinition />
356
+
357
+ </Grid.RowDefinitions>
358
+
359
+ <Label Grid.Row="0" Grid.RowSpan="2" Content="コンボボックス" />
360
+
361
+ </Grid>
362
+
363
+ </DataGridTemplateColumn.Header>
364
+
365
+
366
+
367
+ <DataGridTemplateColumn.CellTemplate>
368
+
369
+ <DataTemplate>
370
+
371
+ <Grid>
372
+
373
+ <Grid.RowDefinitions>
374
+
375
+ <RowDefinition />
376
+
377
+ <RowDefinition />
378
+
379
+ </Grid.RowDefinitions>
380
+
381
+ <ComboBox Grid.Row="0" ItemsSource="{StaticResource TestList}"
382
+
383
+ DisplayMemberPath="Name"
384
+
385
+ SelectedIndex="{Binding ComboIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
386
+
387
+ </ComboBox>
388
+
389
+ <Label Grid.Row="1" />
390
+
391
+ </Grid>
392
+
393
+ </DataTemplate>
394
+
395
+ </DataGridTemplateColumn.CellTemplate>
396
+
397
+ </DataGridTemplateColumn>
398
+
399
+
400
+
401
+ <DataGridTemplateColumn>
402
+
403
+ <DataGridTemplateColumn.Header>
404
+
405
+ <Grid>
406
+
407
+ <Grid.RowDefinitions>
408
+
409
+ <RowDefinition />
410
+
411
+ <RowDefinition />
412
+
413
+ </Grid.RowDefinitions>
414
+
415
+ </Grid>
416
+
417
+ </DataGridTemplateColumn.Header>
418
+
419
+
420
+
421
+ <DataGridTemplateColumn.CellTemplate>
422
+
423
+ <DataTemplate>
424
+
425
+ <Grid>
426
+
427
+ <Grid.RowDefinitions>
428
+
429
+ <RowDefinition />
430
+
431
+ <RowDefinition />
432
+
433
+ </Grid.RowDefinitions>
434
+
435
+ <Button Content="Button" Command="{Binding Path=DataContext.PopUpCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
436
+
437
+ <Label Grid.Row="1" />
438
+
439
+ </Grid>
440
+
441
+ </DataTemplate>
442
+
443
+ </DataGridTemplateColumn.CellTemplate>
444
+
445
+ </DataGridTemplateColumn>
446
+
447
+
448
+
449
+ </DataGrid.Columns>
450
+
451
+ <i:Interaction.Behaviors>
452
+
453
+ <local:BindSelectedItemsBehavior SelectedRowItems="{Binding SelectedItems}" />
454
+
455
+ </i:Interaction.Behaviors>
456
+
457
+ </DataGrid>
458
+
459
+ <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
460
+
461
+ <Button Content="登録" HorizontalAlignment="Center" VerticalAlignment="Top" Width="75" Command="{Binding SaveCommand}" />
462
+
463
+ <Button Content="削除" HorizontalAlignment="Center" VerticalAlignment="Top" Width="75" Command="{Binding DeleteCommand}" />
464
+
465
+ </StackPanel>
466
+
467
+ <TextBox Name="TestText" Height="23" TextWrapping="Wrap" Text=""/>
468
+
469
+ </StackPanel>
470
+
471
+ </Window>
472
+
473
+ ```