WPF DataGrid: Reordering and Editing Rows with GongSolutions.WPF.DragDrop

Viewed 153

I'm using GongSolutions.WPF.DragDrop. What I want is to load a List into a DataGrid, be able to add rows, delete rows, edit rows, and drag drop reorder rows. Then read every item from the list in the new order back into a database.

When I give an item source to a DataGrid, I can't drag drop reorder the rows.

If I add the items through DataGrid.Items.Add to the DataGrid, I can't edit the rows.

Source Code; MainWindow.xaml

<Window
    x:Class="WPFDragDropControls.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:dd="urn:gong-wpf-dragdrop"
    xmlns:local="clr-namespace:WPFDragDropControls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    Loaded="Window_Loaded"
    mc:Ignorable="d">
    <Grid>
        <DataGrid
            Name="DataGrid_DD_Test"
            dd:DragDrop.IsDragSource="True"
            dd:DragDrop.IsDropTarget="True"
            AutoGenerateColumns="False"
            CanUserAddRows="True"
            CanUserDeleteRows="True">
            <DataGrid.Columns>
                <DataGridTextColumn
                    Binding="{Binding ColumnName}"
                    Header="ColumnName"
                    IsReadOnly="False" />
                <DataGridTextColumn
                    Binding="{Binding SystemName}"
                    Header="SystemName"
                    IsReadOnly="False" />
                <DataGridTextColumn
                    Width="*"
                    Binding="{Binding Format}"
                    Header="Format"
                    IsReadOnly="False" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Windows;

namespace WPFDragDropControls{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<TestItems> TestCollection = new();

            TestCollection.Add(new TestItems() { ColumnName = "EmployerSig", SystemName = "Form9061_1a", Format = "trim[32]" });
            TestCollection.Add(new TestItems() { ColumnName = "StartDate", SystemName = "Form9061_6a", Format = "dt:yyyyMMdd" });
            TestCollection.Add(new TestItems() { ColumnName = "State", SystemName = "Form9061_4ga", Format = "trim[2]" });

            DataGrid_DD_Test.ItemsSource = TestCollection;

            //DataGrid_DD_Test.Items.Add(new TestItems() { ColumnName = "StartDate", SystemName = "Form9061_6a", Format = "dt:yyyyMMdd" });
            //DataGrid_DD_Test.Items.Add(new TestItems() { ColumnName = "State", SystemName = "Form9061_4ga", Format = "trim[2]" });
            //DataGrid_DD_Test.Items.Add(new TestItems() { ColumnName = "EmployerSig", SystemName = "Form9061_1a", Format = "trim[32]" });
        }

        class TestItems
        {
            public string ColumnName { get; set; }
            public string SystemName { get; set; }
            public string Format { get; set; }
        }
    }
}

How can I have my cake and eat it too?

0 Answers
Related