C# WPF 工具包:如何使 datagrid 中的单元格可编辑?

5
请注意这个使用Microsoft Visual Studio 2008制作的小型WPF C#程序的代码:
.xaml
<Window x:Class="WpfDatagridTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfToolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <WpfToolkit:DataGrid 
        x:Name="DataGrid_" ItemsSource="{Binding}"
        SelectionMode="Extended"
        CanUserAddRows="False" CanUserDeleteRows="False"
        CanUserResizeRows="False" CanUserSortColumns="False"
        AutoGenerateColumns="False"
        RowHeaderWidth="17" RowHeight="25" />
    </Grid>
</Window>

.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;

namespace WpfDatagridTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            const int MAX = 10;

            for(int i = 0; i < MAX; ++i)
                DataGrid_.Columns.Add(new DataGridTextColumn()
                {
                    Header = i
                });
            DataGrid_.Items.Add("");
        }
    }
}

这个程序展示了一个带有10列标题和一个空行的WPF工具包数据网格。

当选择空行的某个单元格并点击进行编辑时,会出现问题:程序崩溃。在Visual Studio中会弹出一个消息框,显示:“当前位置没有可用的源代码”。我想知道如何使数据网格中的单元格可编辑?


以下是异常和堆栈跟踪信息:

"System.InvalidOperationException"错误未被处理。错误信息为:此视图不允许“EditItem”操作。该错误源于PresentationFramework,堆栈跟踪信息如下:在System.Windows.Controls.ItemCollection中,System.ComponentModel.IEditableCollectionView接口的EditItem方法不可用;在Microsoft.Windows.Controls.DataGrid.cs的3396行,Microsoft.Windows.Controls.DataGrid类的EditRowItem方法执行失败;在Microsoft.Windows.Controls.DataGrid.cs的2208及2036行,Microsoft.Windows.Controls.DataGrid类的OnExecutedBeginEdit方法执行失败;在System.Windows.Input.CommandBinding类的OnExecuted方法中,第二个参数e是ExecutedRoutedEventArgs类型;在System.Windows.UIElement类的OnExecutedThunk方法中,第二个参数e也是ExecutedRoutedEventArgs类型;在Microsoft.Windows.Controls.DataGrid类的BeginEdit方法中,第一个参数editingEventArgs是RoutedEventArgs类型;在Microsoft.Windows.Controls.DataGridCell.cs的748及726行,Microsoft.Windows.Controls.DataGridCell类的OnAnyMouseLeftButtonDown方法执行失败;在System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler方法中,第二个参数e是MouseButtonEventArgs类型。"

2
1.3千次浏览,没有点赞哈哈 :D - Partial
1个回答

3
你正在将你的ItemsSource设置为Binding,但是没有DataContext,所以这个绑定是无效的。然后你在代码中生成列并用临时数据填充它们。从我对DataGrid的理解来看,只有当网格绑定了数据时才支持编辑。否则,你要编辑什么数据呢?
此外,我认为还有一个IsReadOnly属性(请查看DataGrid文档),需要设置为false(但我认为默认情况下它是false)。

我正在生成空列并给这些列添加标题。我要编辑的数据是这些空单元格。 - Partial
能否将数据网格绑定到某种空列表,以便在编辑数据网格单元格时将该单元格中的文本添加到列表中? - Partial
1
当然可以。创建一个新的类来表示您要存储在数据网格中的数据。然后,您可以将其绑定到该数据的ObservableCollection。类似于这样: http://msdn.microsoft.com/en-us/library/ms748365.aspx - Charlie
我尝试过使用ItemsSource和ArrayList。这是一个不好的主意吗? - Partial
1
ArrayList不会像ObservableCollection一样提供自动更改通知。如果您不需要任何更改通知,则ArrayList应该是可以的。您还可以在自己的ArrayList类上实现INotifyPropertyChanged。 - Charlie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接