I have a wpf datagrid which allows users to add rows. I need to add a functionality where in a user can copy a data row and on paste it is added as a new row. I am able to copy(Ctrl+C) a row, but on paste (Ctrl+V), all the items are pasted on to the first cell of the row. How can i paste it into each cell of the row.
<DataGrid Name="grdTest" HorizontalAlignment="Left" Grid.Row="0" Margin="5,5,0,0" VerticalAlignment="Top" Height="426" Width="1034"
AlternationCount="2" ItemsSource="{Binding TestsList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Is Active" Binding="{Binding Path=IsActive,Mode=TwoWay}">
<DataGridTextColumn Width="90">
<DataGridTextColumn.Binding>
<Binding Path="TestName" Mode="TwoWay">
<Binding.ValidationRules>
<localVal:ValidationRules/>
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
<DataGridTextColumn.Header>
<TextBlock Width="80" Text="Test Name" ToolTip=""/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn.Binding>
<Binding Path="TestType" Mode="TwoWay">
<Binding.ValidationRules>
<localVal:ValidationRules/>
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
<DataGridTextColumn.Header>
<TextBlock Width="80" Text="Test Type" ToolTip=""/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
This is what I have got so far:
private void grdTest_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
{
copiedItem = (Test)e.Item;
}
private void grdTest_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.V && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
if (App.ViewModel.UpdatedListCommand.CanExecute(null))
{
App.ViewModel.UpdatedListRowCommand.Execute(copiedItem);
}
}}}
I update the list in the viewmodel and call NotifyProperty changed. The list is updated, however, it doesnt get reflected in the UI. The row ( to which I copy/pasted value) seems empty, but when i select a cell, the value appears. So basically it is being set properly but some how not shown in the datagrid.