首页 > 解决方案 > WPF DataGrid:使用 GongSolutions.WPF.DragDrop 重新排序和编辑行

问题描述

我正在使用 GongSolutions.WPF.DragDrop。我想要的是将列表加载到 DataGrid 中,能够添加行、删除行、编辑行和拖放重新排序行。然后以新顺序将列表中的每个项目读回数据库。

当我将项目源提供给 DataGrid 时,我无法拖放对行重新排序。

如果我通过 DataGrid.Items.Add 将项目添加到 DataGrid,则无法编辑行。

源代码; 主窗口.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>

主窗口.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; }
        }
    }
}

我怎样才能有我的蛋糕,也吃呢?

标签: c#xaml

解决方案


推荐阅读