注意为与做这个小WPF C#程序代码的微软Visual Studio 2008中:

<强>的.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.EditRowItem(对象   rowItem)在   C:\ DD \ WPF_1的\ src \ WPF的\ src \ ControlsPack \ WPFToolkit \ DataGrid中\微软\的Windows \控件\ DataGrid.cs:行   3396          在Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs   e)在   C:\ DD \ WPF_1的\ src \ WPF的\ src \ ControlsPack \ WPFToolkit \ DataGrid中\微软\的Windows \控件\ DataGrid.cs:行   2208          在Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(对象   发件人,ExecutedRoutedEventArgs e)在   C:\ DD \ WPF_1的\ src \ WPF的\ src \ ControlsPack \ WPFToolkit \ DataGrid中\微软\的Windows \控件\ DataGrid.cs:行   2036          在System.Windows.Input.CommandBinding.OnExecuted(对象   发件人,ExecutedRoutedEventArgs E)          在System.Windows.Input.CommandManager.ExecuteCommandBinding(对象   发件人,ExecutedRoutedEventArgs E,   的CommandBinding的CommandBinding)          在System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection   化CommandBindings,对象发件人,   RoutedEventArgs E,ICommand的命令,   布尔执行)          在System.Windows.Input.CommandManager.FindCommandBinding(对象   发件人,RoutedEventArgs E,ICommand的   命令,执行布尔)          在System.Windows.Input.CommandManager.OnExecuted(对象   发件人,ExecutedRoutedEventArgs E)          在System.Windows.UIElement.OnExecutedThunk(对象   发件人,ExecutedRoutedEventArgs E)          在System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(代表   genericHandler,对象目标)          在System.Windows.RoutedEventArgs.InvokeHandler(代表   处理程序,对象目标)          在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象   目标,RoutedEventArgs   routedEventArgs)          在System.Windows.EventRoute.InvokeHandlersImpl(对象   源,RoutedEventArgs指定参数时,布尔   再加注)          在System.Windows.UIElement.RaiseEventImpl(DependencyObject的   发件人,RoutedEventArgs参数)          在System.Windows.UIElement.RaiseEvent(RoutedEventArgs   ARGS,布尔信任)          在System.Windows.Input.RoutedCommand.ExecuteImpl(对象   参数,IInputElement目标,   布尔用户引发)          在System.Windows.Input.RoutedCommand.Execute(对象   参数,IInputElement目标)          在Microsoft.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs   editingEventArgs)在   C:\ DD \ WPF_1的\ src \ WPF的\ src \ ControlsPack \ WPFToolkit \ DataGrid中\微软\的Windows \控件\ DataGrid.cs:行   3059          在Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs   e)在   C:\ DD \ WPF_1的\ src \ WPF的\ src \ ControlsPack \ WPFToolkit \ DataGrid中\微软\的Windows \控件\ DataGridCell.cs:行   748          在Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(对象   发件人,MouseButtonEventArgs e)在   C:\ DD \ WPF_1的\ src \ WPF的\ src \ ControlsPack \ WPFToolkit \ DataGrid中\微软\的Windows \控制\ DataGridCell.cs:林Ë   726          在System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(代表   genericHandler,对象genericTarget)          在System.Windows.RoutedEventArgs.InvokeHandler(代表   处理程序,对象目标)          在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象   目标,RoutedEventArgs   routedEventArgs)          在System.Windows.EventRoute.InvokeHandlersImpl(对象   源,RoutedEventArgs指定参数时,布尔   再加注)          在System.Windows.UIElement.ReRaiseEventAs(DependencyObject的   发件人,RoutedEventArgs指定参数时,   RoutedEvent newEvent)          在System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject的   发件人,MouseButtonEventArgs E)          在System.Windows.UIElement.OnMouseDownThunk(对象   发件人,MouseButtonEventArgs E)          在System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(代表   genericHandler,对象genericTarget)          在System.Windows.RoutedEventArgs.InvokeHandler(代表   处理程序,对象目标)          在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象   目标,RoutedEventArgs   routedEventArgs)          在System.Windows.EventRoute.InvokeHandlersImpl(对象   源,RoutedEventArgs指定参数时,布尔   再加注)          在System.Windows.UIElement.RaiseEventImpl(DependencyObject的   发件人,RoutedEventArgs参数)          在System.Windows.UIElement.RaiseEvent(RoutedEventArgs   ARGS,布尔信任)          在System.Windows.Input.InputManager.ProcessStagingArea()          在System.Windows.Input.InputManager.ProcessInput(InputEventArgs   输入)          在System.Windows.Input.InputProviderSite.ReportInput(InputReport   inputReport)          在System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr的   HWND,INPUTMODE模式的Int32时间戳,   RawMouseActions行动的Int32 X,   INT32 Y,INT32轮)          在System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr的   HWND,味精的Int32,IntPtr的wParam中,IntPtr的   lParam的,布尔逻辑处理)          在System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr的   HWND,味精的Int32,IntPtr的wParam中,IntPtr的   lParam的,布尔逻辑处理)          在MS.Win32.HwndWrapper.WndProc(IntPtr的   HWND,味精的Int32,IntPtr的wParam中,IntPtr的   lParam的,布尔逻辑处理)          在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象   O)          在System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表   回调,对象指定参数时,布尔   isSingleParameter)          在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象   源,代表回调,对象   ARGS,布尔isSingleParameter,   代表catchHandler)          在System.Windows.Threading.Dispatcher.WrappedInvoke(代表   回调,对象指定参数时,布尔   isSingleParameter,委托   catchHandler)          在System.Windows.Threading.Dispatcher.InvokeImpl(的DispatcherPriority   优先级,时间跨度超时,委托   法,对象指定参数时,布尔   isSingleParameter)          在System.Windows.Threading.Dispatcher.Invoke(的DispatcherPriority   优先级,代表方法,对象ARG)          在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr的HWND,味精的Int32,IntPtr的wParam中,IntPtr的   lParam的)          在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&   MSG)          在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame   帧)          在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame   帧)          在System.Windows.Threading.Dispatcher.Run()          在System.Windows.Application.RunDispatcher(对象   忽视)          在System.Windows.Application.RunInternal(窗口   窗口)          在System.Windows.Application.Run(窗口   窗口)          在System.Windows.Application.Run()          在WpfDatagridTest.App.Main()中   C:\ Users \用户弗兰克·\文件\ Visual_Studio_2008 \项目\ WpfDatagridTest \ WpfDatagridTest \ OBJ \调试\ App.g.cs:线   0          在System.AppDomain。_nExecuteAssembly(大会   组件,字串[] args)          在System.AppDomain.ExecuteAssembly(字符串   assemblyFile,证据   assemblySecurity,字串[] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ThreadHelper.ThreadStart_Context(对象   州)          在System.Threading.ExecutionContext.Run(执行上下文   执行上下文,ContextCallback   回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()   的InnerException:

有帮助吗?

解决方案

您在您的ItemsSource设置的绑定的,但目前还没有这样的DataContext这种结合是没用的。那么你在代码隐藏生成的列和临时数据填充。据我了解有关DataGrid,如果存在网格绑定到数据编辑时,才支持。否则,什么样的数据是你编辑?

此外,我相信这是一个的 IsReadOnly 的属性(检查DataGrid的文档),需要被设置为false(但我认为这是默认为false)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top