環境
- VB
- WPF
DataGridに最初新規行が表示されている状態で、その新規行を編集すると新たな新規行を追加したいのですがやり方が分かりません。
どなたか分かる方がいらしたら教えて頂きたいです。
追加
DataGridの機能で新規行を追加するとNewItemPlaceholderになります。Modelに紐づいた新規行を追加したです。
Public Class Window1ViewModel Public Sub New() Me.DataCollection.Add(New Model() With { .ComboIndex = 2, .No = 1, .Test1 = "1", .Test2 = "11", .Test3 = "111"}) Me.DataCollection.Add(New Model() With { .ComboIndex = 0, .No = 2, .Test1 = "2", .Test2 = "22", .Test3 = "222"}) End Sub End Class Public Class TestList Inherits List(Of ComboModel) Public Sub New() Me.Add(New ComboModel(0, "データ1")) Me.Add(New ComboModel(1, "データ2")) Me.Add(New ComboModel(2, "データ3")) End Sub End Class Public Class Model Inherits ViewModelBase Private _ComboIndex As String Public Property ComboIndex() As String Get Return _ComboIndex End Get Set(ByVal value As String) _ComboIndex = value End Set End Property Private _No As Integer Public Property No() As Integer Get Return _No End Get Set(ByVal value As Integer) _No = value OnPropertyChanged("No") End Set End Property Private _Test1 As String Public Property Test1() As String Get Return _Test2 End Get Set(ByVal value As String) _Test1 = value OnPropertyChanged("Test1") End Set End Property Private _Test2 As String Public Property Test2() As String Get Return _Test2 End Get Set(ByVal value As String) _Test2 = value OnPropertyChanged("Test2") End Set End Property Private _Test3 As String Public Property Test3() As String Get Return _Test3 End Get Set(ByVal value As String) _Test3 = value OnPropertyChanged("Test3") End Set End Property End Class
<Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:Sample" Title="Window1" Height="400" Width="650"> <Window.Resources> <local:TestList x:Key="TestList" /> </Window.Resources> <Window.DataContext> <local:Window1ViewModel/> </Window.DataContext> <StackPanel Orientation="Vertical"> <DataGrid Grid.Row="1" Name="DataGrid1" ItemsSource="{Binding DataCollection}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" AutoGenerateColumns="False" Height="300" SelectionMode="Single" > <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.Header> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Label Grid.Row="0" Grid.RowSpan="2" Content="てすと1" /> </Grid> </DataGridTemplateColumn.Header> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBox Grid.Row="0" Grid.RowSpan="2" Text="{Binding Test1}" /> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn> <DataGridTemplateColumn.Header> <DataGridColumnHeader> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Label Grid.Row="0" Content="てすと2" /> <Label Grid.Row="1" Content="てすと3" /> </Grid> </DataGridColumnHeader> </DataGridTemplateColumn.Header> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <TextBox Grid.Row="0" Text="{Binding Test2, UpdateSourceTrigger=PropertyChanged}" /> <TextBox Grid.Row="1" Text="{Binding Test3, UpdateSourceTrigger=PropertyChanged}" /> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn> <DataGridTemplateColumn.Header> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Label Grid.Row="0" Grid.RowSpan="2" Content="コンボボックス" /> </Grid> </DataGridTemplateColumn.Header> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <ComboBox Grid.Row="0" ItemsSource="{StaticResource TestList}" DisplayMemberPath="Name" SelectedIndex="{Binding ComboIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" > </ComboBox> <Label Grid.Row="1" /> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn> <DataGridTemplateColumn.Header> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> </Grid> </DataGridTemplateColumn.Header> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Button Content="Button" Command="{Binding Path=DataContext.PopUpCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" /> <Label Grid.Row="1" /> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> <i:Interaction.Behaviors> <local:BindSelectedItemsBehavior SelectedRowItems="{Binding SelectedItems}" /> </i:Interaction.Behaviors> </DataGrid> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <Button Content="登録" HorizontalAlignment="Center" VerticalAlignment="Top" Width="75" Command="{Binding SaveCommand}" /> <Button Content="削除" HorizontalAlignment="Center" VerticalAlignment="Top" Width="75" Command="{Binding DeleteCommand}" /> </StackPanel> <TextBox Name="TestText" Height="23" TextWrapping="Wrap" Text=""/> </StackPanel> </Window>
何がわからないのでしょうか。新規行についてはWPFでは特別なことをしなくてもデフォルトで動作するはずです。
問題のあるコードをまず提示してください。