【发布时间】:2010-11-13 03:55:49
【问题描述】:
当我尝试使用 datagrid.items.add() 将项目从一个数据网格添加到另一个数据网格时遇到问题。基本上我有两个数据网格在主从关系中起作用。 First DataGrid1 用于显示自动生成的列和行。第二个数据网格 DataGrid2 将在单击特定按钮时显示 DataGrid1.SelectedItems。每次单击按钮时,我都希望从 DataGrid1 中选择的项目留在 DataGrid2 中,并且每次单击按钮时,都会将更多项目添加到 DataGrid2 中。除了能够在 DataGrid2 上编辑单元格之外,我已经能够完成我的大部分要求。当我双击 DataGrid2 中的一个单元格时,我得到一个异常,上面写着“EditItem' is not allowed for this view”。我已经阅读了很多关于将数据添加到 ObservableCollection、ListCollectionView 等的帖子,但要么我无法以正确的方式实现它们,要么不适用于我的情况。我的代码如下,顺便提一下
<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>
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Windows;
使用 System.Windows.Controls;
使用 System.Windows.Data;
使用 System.Windows.Documents;
使用 System.Windows.Input;
使用 System.Windows.Media;
使用 System.Windows.Media.Imaging;
使用 System.Windows.Navigation;
使用 System.Windows.Shapes;
命名空间 WpfApplication1
{
///
/// MainWindow.xaml 的交互逻辑
///
公共部分类 MainWindow : 窗口
{
公共主窗口()
{
初始化组件();
dataGrid1.ItemsSource = idata;
}
私人无效按钮1_Click(对象发送者,RoutedEventArgs e)
{
foreach(dataGrid1.SelectedItems 中的 latlongobj 项)
{
dataGrid2.Items.Add(item);
}
}
}
}
使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 命名空间 WpfApplication1 { 类 latlonobj { 公共字符串名称 { 获取;放; } 公共双纬度{得到;放; } 公共双隆{得到;放; } } }
【问题讨论】:
标签: c# wpf datagrid wpftoolkit wpfdatagrid