简体   繁体   English

C#WPF工具包:如何​​使数据网格中的单元格可编辑?

[英]C# WPF toolkit: How can I make a cell from a datagrid be editable?

Take note of the code for this small WPF C# program made with Microsoft Visual Studio 2008: 记下使用Microsoft Visual Studio 2008制作的这个小型WPF C#程序的代码:

.xaml 的.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 的.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("");
        }
    }
}

This program simply shows a WPF toolkit datagrid with 10 columns with headers and an empty row. 该程序只显示一个WPF工具包datagrid,其中包含10个带标题和空行的列。

There is problem that occurs when a certain cell of the empty row is selected and then clicked to be edited: the program crashes. 当选择空行的某个单元格然后单击进行编辑时会出现问题:程序崩溃。 A message box shows up in Visual Studio saying this: "There is no source code available for the current location." Visual Studio中显示一个消息框,上面写着:“当前位置没有可用的源代码。” I would like to know how can I make a cell from a datagrid be editable? 我想知道如何使数据网格中的单元格可编辑?


Here is the exception and stacktrace: 这是异常和堆栈跟踪:

System.InvalidOperationException was unhandled Message="'EditItem' is not allowed for this view." System.InvalidOperationException未处理Message =“'EditItem'不允许此视图。”
Source="PresentationFramework" 来源= “PresentationFramework”
StackTrace: at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) at Microsoft.Windows.Controls.DataGrid.EditRowItem(Object rowItem) in C:\\dd\\WPF_1\\src\\wpf\\src\\ControlsPack\\WPFToolkit\\DataGrid\\Microsoft\\Windows\\Controls\\DataGrid.cs:line 3396 at Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e) in C:\\dd\\WPF_1\\src\\wpf\\src\\ControlsPack\\WPFToolkit\\DataGrid\\Microsoft\\Windows\\Controls\\DataGrid.cs:line 2208 at Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender, ExecutedRoutedEventArgs e) in C:\\dd\\WPF_1\\src\\wpf\\src\\ControlsPack\\WPFToolkit\\DataGrid\\Microsoft\\Windows\\Controls\\DataGrid.cs:line 2036 at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e) at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding) at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindin StackTrace:位于C:\\ dd \\ WPF_1 \\ src \\ wpf \\ src \\中的Microsoft.Windows.Controls.DataGrid.EditRowItem(Object rowItem)中的System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) ControlsPack \\ WPFToolkit \\ DataGrid \\ Microsoft \\ Windows \\ Controls \\ DataGrid.cs:C:\\ dd \\ WPF_1 \\ src \\ wpf \\ src \\ ControlsPack \\ WPFToolkit \\中的Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)中的第3396行DataGrid \\ Microsoft \\ Windows \\ Controls \\ DataGrid.cs:C:\\ dd \\ WPF_1 \\ src \\ wpf \\ src \\ ControlsPack \\ WPFToolkit \\ DataGrid中的Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender,ExecutedRoutedEventArgs e)中的第2208行\\ Microsoft \\ Windows \\ Controls \\ DataGrid.cs:System.Windows.Input.CommandBinding.OnExecuted(Object sender,ExecutedRoutedEventArgs e)中的第2036行,位于System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender,ExecutedRoutedEventArgs e,CommandBinding commandBinding)在System.Windows.Input.CommandManager.FindCommandBinding(CommandBindin gCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute) at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute) at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e) at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e) at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) at System.Windows.Input.RoutedComma System.Windows.Input.CommandManager.OnExecuted(Object)上的System.Windows.Input.CommandManager.FindCommandBinding(Object sender,RoutedEventArgs,ICommand命令,布尔执行)中的gCollection commandBindings,Object sender,RoutedEventArgs e,ICommand命令,布尔执行)发送者,ExecutedRoutedEventArgs e)在System.Windows.RindtedEventArgs.InvokeHandler(委托处理程序,对象)的System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler,Object target)中的System.Windows.UIElement.OnExecutedThunk(Object sender,ExecutedRoutedEventArgs e)处。在System.Windows.Uindlement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)的System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)上的System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)中的目标) System.Windows.Input.RoutedComma上的System.Windows.UIElement.RaiseEvent(RoutedEventArgs args,布尔值信任) nd.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated) at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target) at Microsoft.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs editingEventArgs) in C:\\dd\\WPF_1\\src\\wpf\\src\\ControlsPack\\WPFToolkit\\DataGrid\\Microsoft\\Windows\\Controls\\DataGrid.cs:line 3059 at Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e) in C:\\dd\\WPF_1\\src\\wpf\\src\\ControlsPack\\WPFToolkit\\DataGrid\\Microsoft\\Windows\\Controls\\DataGridCell.cs:line 748 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:line 726 at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.Routed 在C:\\ dd \\中的Microsoft.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs editingEventArgs)的System.Windows.Input.RoutedCommand.Execute(Object parameter,IInputElement target)中的nd.ExecuteImpl(Object parameter,IInputElement target,Boolean userInitiated) WPF_1 \\ src \\ wpf \\ src \\ ControlsPack \\ WPFToolkit \\ DataGrid \\ Microsoft \\ Windows \\ Controls \\ DataGrid.cs:C:\\ dd \\ WPF_1 \\ src \\中Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e)中的第3059行wpf \\ src \\ ControlsPack \\ WPFToolkit \\ DataGrid \\ Microsoft \\ Windows \\ Controls \\ DataGridCell.cs:位于C:\\ dd \\ WPF_1 \\ src \\ wpf中的Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(Object sender,MouseButtonEventArgs e)中的第748行\\ SRC \\ ControlsPack \\ WPFToolkit \\ DataGrid中\\微软\\的Windows \\控制\\ DataGridCell.cs:行726 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(代表genericHandler,对象genericTarget)在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序, System.Windows.Routed上的对象目标 EventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e) at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UI System.Windows上的System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender,RoutedEventArgs args,RoutedEvent newEvent)上的System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)中的EventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)位于System.Windows.RindtedEventArgs的System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)的System.Windows.UIElement.OnMouseDownThunk(Object sender,MouseButtonEventArgs e)中的.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender,MouseButtonEventArgs e)。位于System.Windows.Uindlement.RaiseEventImpl的System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)的System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)中的InvokeHandler(委托处理程序,Object目标)( System.Windows.UI上的DependencyObject sender,RoutedEventArgs args) Element.RaiseEvent(RoutedEventArgs args, Boolean trusted) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) at System.Windows.T System.Windows.Input.InputManager.ProcessInput中的System.Windows.InputManager.ProcessStagingArea()处于System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)的System.RaiseEvent(RoutedEventArgs args,布尔值受信任)在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的位于MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr)的System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling)中的wParam,IntPtr lParam,Boolean&handling) System.Windows.T上System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,Object args,Boolean isSingleParameter)的MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)中的wParam,IntPtr lParam,Boolean&handling) hreading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter) at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.Run() at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System hreading.ExceptionWrapper.TryCatchWhen在System.Windows.Threading.Dispatcher.WrappedInvoke(对象源,代表回调,对象指定参数时,布尔isSingleParameter,代表catchHandler)(代表回调,对象指定参数时,布尔isSingleParameter,代表catchHandler)在System.Windows.Threading程序.Dispatcher.InvokeImpl(的DispatcherPriority优先权,时间跨度超时,委托方法,对象指定参数时,布尔isSingleParameter)在System.Windows.Threading.Dispatcher.Invoke(的DispatcherPriority优先权,委托方法,对象ARG)在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr的在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)的System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)的MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg)中的hwnd,Int32消息,IntPtr wParam,IntPtr lParam) )在系统的System.Windows.Application.RunInternal(窗口窗口)的System.Windows.Application.RunDispatcher(Object ignore)处的System.Windows.Threading.Dispatcher.Run()处 .Windows.Application.Run(Window window) at System.Windows.Application.Run() at WpfDatagridTest.App.Main() in C:\\Users\\Frank\\Documents\\Visual_Studio_2008\\Projects\\WpfDatagridTest\\WpfDatagridTest\\obj\\Debug\\App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.Windows.Application.Run()中的.Windows.Application.Run(窗口窗口)位于C:\\ Users \\ Frank \\ Documents \\ Visual_Studio_2008 \\ Projects \\ WpfDatagridTest \\ WpfDatagridTest \\ _ obj \\ Debug \\中的WpfDatagridTest.App.Main() App.g.cs:System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)的第0行,位于Microsoft.VisualStudio.HostingProcess.HostProc的System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)。在System.Threading.Thread.ThreadStart()的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)的System.Threading.ThreadHelper.ThreadStart_Context(Object state)中的RunUsersAssembly()InnerException:

You are setting your ItemsSource to Binding , but there is no DataContext so this binding is useless. 您将ItemsSource设置为Binding ,但没有DataContext,因此此绑定无用。 Then you are generating the columns in the code-behind and filling them with temporary data. 然后,您将在代码隐藏中生成列并使用临时数据填充它们。 From what I understand about the DataGrid, editing is only supported if there is data that the grid is bound to. 根据我对DataGrid的理解,只有在网格绑定的数据时才支持编辑。 Otherwise, what data are you editing? 否则,您正在编辑哪些数据?

Furthermore, I believe there is a IsReadOnly property (check the DataGrid documentation), which needs to be set to false (but I think it is false by default). 此外,我相信有一个IsReadOnly属性(检查DataGrid文档),需要将其设置为false(但我认为默认情况下它是false)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM