2009-08-04 61 views
4

注意为与Microsoft Visual Studio 2008年提出这个小WPF C#程序代码:C#WPF工具包:如何​​让数据网格中的单元格可编辑?

的.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:行在Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs 五) C:\ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs:line Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender,ExecutedRoutedEventArgs e) C:\ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs:在System.Windows.Input.CommandBinding.OnExecuted(对象 发件人,ExecutedRoutedEventArgs E) 线在System.Windows.Input.CommandManager.ExecuteCommandBinding(对象 发件人,ExecutedRoutedEventArgs E, 的CommandBinding的CommandBinding) 在System.Windows .Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings,Object sender, 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 指定参数时,布尔信任) 在System.Windows.Input.RoutedCommand.ExecuteImpl(对象 参数,IInputElement目标, 布尔userInitiated) 在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:行在Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs E)在 C:\ DD \ WPF_1的\ src \ WPF的\ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGridCell.cs:行 at Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(Object sender,MouseButtonEventArgs e)in C:\ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGridCell.cs:行 at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget) 在System.Windows.RoutedEventArgs.InvokeHandler(代表 处理程序,对象目标) 在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象 目标,routedEventArgs routedEventArgs) 在System.Windows.EventRoute.InvokeHandlersImpl(对象 源,RoutedEventArgs参数,布尔型 重新编号) at 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.Eve ntRoute.InvokeHandlersImpl(对象 源,RoutedEventArgs指定参数时,布尔 再加注) 在System.Windows.UIElement.RaiseEventImpl(DependencyObject的 发件人,RoutedEventArgs参数) 在System.Windows.UIElement.RaiseEvent(RoutedEventArgs 指定参数时,布尔信任) 在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,MSG的Int32,IntPtr的wParam中,IntPtr的 lParam的,布尔&处理) 在System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr的 HWND,MSG的Int32,IntPtr的wParam中,IntPtr的 lParam的,布尔&处理) 在MS.Win32.HwndWrapper.WndProc(IntPtr的 HWND, Int32 msg,IntPtr wParam,IntPtr lParam,布尔型&处理) 在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 O) 在System.Windows.Threading.ExceptionWrapper.InternalRealCall在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象 (代表 回调,对象指定参数时,布尔 isSingleParameter) 源,代表回调,对象 指定参数时,布尔isSingleParameter, 代表catchHandler) 在System.Windows.Threading.Dispatcher.WrappedInvoke(代表 回调,对象指定参数时,布尔 isSingleParameter,代表 catchHandler) 在System.Windows.Threading程序.Dispatcher.InvokeImpl(DispatcherPriority 的优先级,时间跨度超时,代表 方法中,在System.Windows.Threading.Dispatcher.Invoke(的DispatcherPriority 优先权,委托方法,对象ARG) 在MS.Win32.HwndSubclass.SubclassWndProc对象指定参数时,布尔 isSingleParameter) (IntPtr的HWND ,MSG的Int32,IntPtr的wParam中,IntPtr的 LPARAM) 在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG & MSG) 在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 帧) 在System.Windows.Threading程序。 Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.Run() 在System.Windows.Application.RunDispatcher(对象 忽略) 在System.Windows.Application.RunInternal(窗口 窗口) 在System.Windows.Application.Run(窗口 窗口) 在System.Windows.Application。在WpfDatagridTest.App.Main()在 C运行() :\ Users \用户弗兰克·\文件\ Visual_Studio_2008 \项目\ WpfDatagridTest \ WpfDatagridTest \ OBJ \调试\ App.g.cs:线在System.AppDomain。 _nExecuteAssembly(Assembly assembly,String [] args) at System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] a RGS) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象 状态) 在System.Threading.ExecutionContext.Run(的ExecutionContext 的ExecutionContext,ContextCallback 回调,对象状态) 在System.Threading.ThreadHelper.ThreadStart() 的InnerException:

+2

1.3 k的意见,并没有upvote lol:D – Partial 2010-01-30 21:13:44

回答

3

你设置你的ItemsSource到绑定,但目前还没有这样的DataContext这种结合是没用的。然后,您在代码隐藏中生成列并填充临时数据。根据我对DataGrid的了解,仅当网格绑定数据时才支持编辑。否则,你在编辑什么数据?此外,我相信有一个IsReadOnly属性(检查DataGrid文档),需要将其设置为false(但我认为它默认为false)。

+0

我生成空列,并给这些列标题。我正在编辑的数据是空单元格。 – Partial 2009-08-04 22:45:38

相关问题