首页 文章

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

提问于
浏览
4

记下使用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("");
        }
    }
}

该程序只显示一个WPF工具包datagrid,其中包含10个带 Headers 和空行的列 .

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


这是异常和堆栈跟踪:

System.InvalidOperationException未处理Message =“'EditItem'不允许此视图 . ” Source =“PresentationFramework”StackTrace:位于C:\ dd \ WPF_1 \ src中的Microsoft.Windows.Controls.DataGrid.EditRowItem(Object rowItem)中的System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs:C:\ dd \ WPF_1 \ src \ wpf \ src中的Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)中的第3396行\ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs:C:\ dd \ WPF_1 \ src \ wpf \ src \中的Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender,ExecutedRoutedEventArgs e)中的第2208行ControlsPack \ WPFToolkit \ DataGrid \ 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.F System.Windows.Input.CommandManager.OnExecuted的System.Windows.Input.CommandManager.FindCommandBinding(Object sender,RoutedEventArgs,ICommand命令,布尔执行)中的indCommandBinding(CommandBindingCollection commandBindings,Object sender,RoutedEventArgs,ICommand命令,布尔执行) (Object sender,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)的System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)上的System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)中的,Object target) args)在S的System.Windows.UIElement.RaiseEvent(RoutedEventArgs args,布尔值信任)在Microsoft.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs editingEventArgs)的System.Windows.Input.RoutedCommand.Execute(Object parameter,IInputElement target)中的ystem.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter,IInputElement target,Boolean userInitiated)在C:\ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs:C:\中的Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e)中的第3059行dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGridCell.cs:位于C:\ dd的Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(Object sender,MouseButtonEventArgs e)中的第748行System.Windows.RoutedEventArgs中的System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)中的\ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGridCell.cs:第726行.InvokeHandler(委托处理程序,对象t arget)在System.Windows.EindRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)的System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)处于System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender,RoutedEventArgs args,处于System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)的System.Windows.UIElement.OnMouseDownThunk(Object sender,MouseButtonEventArgs e)中的System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender,MouseButtonEventArgs e)中的RoutedEvent newEvent)在System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象在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.Uput上有System.Windows.Uputlement.RaiseEvent(RoutedEventArgs args,布尔值为受信任),System.Windows.InputManager.ProcessStagingArea()位于System.Windows.Input.InputProviderSite的System.Windows.Input.InputManager.ProcessInput(InputEventArgs输入) . System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd)中的System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd,InputMode模式,Int32时间戳,RawMouseActions操作,Int32 x,Int32 y,Int32轮)中的ReportInput(InputReport inputReport) System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,B),Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling)在MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)的System.Windows.Threading.ExceptionWrapper.InternalRealCall上的MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling)处理oolean&handling)在System.Windows.Threading.Dispatcher.WrappedInvoke的System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source,Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler)中委托回调,Object args,Boolean isSingleParameter(委托回调,对象)在System.Windows.Threading.Dispatcher.Invoke的System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Boolean isSingleParameter)中的args,Boolean isSingleParameter,Delegate catchHandler(DispatcherPriority priority,Delegate方法) ,对象arg)在MS.Win32.UnsafeNativeMethods.DispatchMessage的MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)上 . (MSG& msg)位于System.Windows.Application.RunDispatcher的System.Windows.Threading.Dispatcher.Run()的System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)的System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架) System.Windows.Application.Run(窗口窗口)的System.Windows.Application.RunInternal(窗口窗口)中的System.Windows.Application.Run()处于System对象忽略(对象忽略),位于C:\中的WpfDatagridTest.App.Main()处的System.Windows.Application.Run()用户\ Frank \ Documents \ Visual_Studio_2008 \ Projects \ WpfDatagridTest \ WpfDatagridTest \ obj \ Debug \ App.g.cs:System.AppDomain.ExecuteAssembly上的System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)中的第0行(String assemblyFile ,证据assemblySecurity,String [] args)在System.Threading.ExecutionContext.Run上的System.Threading.ThreadHelper.ThreadStart_Context(Object state)中的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()(ExecutionContext executionContext,ContextCallback回调,对象状态) )在System.Threading.ThreadHelper . ThreadStart()InnerException:

1 回答

  • 3

    您将ItemsSource设置为Binding,但没有DataContext,因此此绑定无用 . 然后,您将在代码隐藏中生成列并使用临时数据填充它们 . 根据我对DataGrid的理解,只有在网格绑定的数据时才支持编辑 . 否则,您正在编辑哪些数据?

    此外,我相信有一个IsReadOnly属性(检查DataGrid文档),需要将其设置为false(但我认为默认情况下为false) .

相关问题