C# 如何将一个项目从一个数据网格添加到另一个数据网格,然后在WPF中编辑它

C# 如何将一个项目从一个数据网格添加到另一个数据网格,然后在WPF中编辑它,c#,wpf,datagrid,wpftoolkit,wpfdatagrid,C#,Wpf,Datagrid,Wpftoolkit,Wpfdatagrid,尝试使用datagrid.items.add将项目从一个datagrid添加到另一个datagrid时出现问题。基本上,我有两个以主从关系运行的数据网格。第一个DataGrid1用于显示自动生成的列和行。单击特定按钮时,第二个datagrid DataGrid2将显示DataGrid1.SelectedItems。每次单击该按钮时,我都希望DataGrid1中的选定项目保留在DataGrid2中,并且每次单击该按钮时,更多的项目被添加到DataGrid2中。除了能够在DataGrid2上编辑单元

尝试使用datagrid.items.add将项目从一个datagrid添加到另一个datagrid时出现问题。基本上,我有两个以主从关系运行的数据网格。第一个DataGrid1用于显示自动生成的列和行。单击特定按钮时,第二个datagrid DataGrid2将显示DataGrid1.SelectedItems。每次单击该按钮时,我都希望DataGrid1中的选定项目保留在DataGrid2中,并且每次单击该按钮时,更多的项目被添加到DataGrid2中。除了能够在DataGrid2上编辑单元格之外,我已经能够完成我的大部分需求。当我双击DataGrid2中的一个单元格时,我得到一个异常,表示此视图不允许使用EditItem。我读过很多关于向ObservaleCollection、ListCollectionView等添加数据的帖子,但要么我不能以正确的方式实现它们,要么我的情况不适用。我的代码如下,顺便提一下,请提前通知thx

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid AutoGenerateColumns="False" Height="77" HorizontalAlignment="Left" Margin="27,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="464" />
    <Button Content="AddRow" Height="23" HorizontalAlignment="Left" Margin="27,107,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <DataGrid AutoGenerateColumns="False" Height="140" HorizontalAlignment="Left" Margin="27,159,0,0" Name="dataGrid2" VerticalAlignment="Top" Width="464" />
</Grid>
这是你想要的吗

公共部分类主窗口:窗口 { ObservableCollection dataGrid2Items=新的ObservableCollection

    public MainWindow()
    {
        InitializeComponent();
        dataGrid1.ItemsSource = idata;
        dataGrid2.ItemsSource = dataGrid2Items;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        foreach (latlongobj item in dataGrid1.SelectedItems)
        {
            if( !dataGrid2Items.Contains( item ) )
                dataGrid2Items.Add(item);
        }

    }

    private ObservableCollection<latlongobj> idata = new ObservableCollection<latlongobj>
        {
            new latlongobj{ name = "n1", Lat = 1, Lon = 2 },
            new latlongobj{ name = "n2", Lat = 2, Lon = 3 },
            new latlongobj{ name = "n3", Lat = 4, Lon = 5 },
        };

@大卫,如果这是答案,你为什么不投票或接受答案!? using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WpfApplication1 { class latlonobj { public string name { get; set; } public double Lat { get; set; } public double Lon { get; set; } } }
    public MainWindow()
    {
        InitializeComponent();
        dataGrid1.ItemsSource = idata;
        dataGrid2.ItemsSource = dataGrid2Items;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        foreach (latlongobj item in dataGrid1.SelectedItems)
        {
            if( !dataGrid2Items.Contains( item ) )
                dataGrid2Items.Add(item);
        }

    }

    private ObservableCollection<latlongobj> idata = new ObservableCollection<latlongobj>
        {
            new latlongobj{ name = "n1", Lat = 1, Lon = 2 },
            new latlongobj{ name = "n2", Lat = 2, Lon = 3 },
            new latlongobj{ name = "n3", Lat = 4, Lon = 5 },
        };