代码之家  ›  专栏  ›  技术社区  ›  Partial

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

  •  4
  • Partial  · 技术社区  · 15 年前

    请注意使用Microsoft Visual Studio 2008制作的这个小型WPF C#程序的代码:

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

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


    以下是异常和堆栈跟踪:

    System.InvalidOperationException为 允许此视图。”
    Source=“PresentationFramework”
    (项目) 在Microsoft.Windows.Controls.DataGrid.EditRowItem(对象 3396 在Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e) 在 2208 在Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(对象 发件人,已在中执行RoutedEventTargets e) C:\dd\WPF\u 1\src\WPF\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 2036 位于System.Windows.Input.CommandBinding.Onexed(对象 发送方,已执行路由目标(e) 发送方,已执行路由目标e, 命令绑定(CommandBinding) 命令绑定、对象发送器、, 路由目标e,ICommand命令, 布尔值(执行) 位于System.Windows.Input.CommandManager.FindCommandBinding(对象 发送方,路由目标e,ICommand 命令,布尔值执行) 在System.Windows.Input.CommandManager.OnExecuted(对象 发送方,已执行路由目标(e) 在System.Windows.UIElement.onexecutedtunk(对象 发送方,已执行路由目标(e) 在System.Windows.Input.ExecutedEventTargets.InvokeeEventHandler(委托 通用处理程序(对象目标) 在System.Windows.RoutedEventArgs.InvokeHandler(委托 处理程序(对象目标) 在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象 目标,路线目标 路线(目标) 在System.Windows.EventRoute.InvokeHandlerSimp(对象 源,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(目标) 在中编辑EventArgs) C:\dd\WPF\u 1\src\WPF\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 3059 e) 在 C:\dd\WPF\u 1\src\WPF\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGridCell.cs:line 在Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(对象 发送器,鼠标按钮输入 726 在System.Windows.Input.MouseButtonEventArgs.InvokeeEventHandler(委托 genericHandler,对象genericTarget) 在System.Windows.RoutedEventArgs.InvokeHandler(委托 处理程序(对象目标) 在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象 在System.Windows.EventRoute.InvokeHandlerSimp(对象 源,RoutedEventArgs参数,布尔值 (重播) 在System.Windows.UIElement.ReRaiseEventAs(DependencyObject 发送方,RoutedEventArgs, 发送器,鼠标按钮(Ventargs e) 位于System.Windows.UIElement.OnMouseDownThunk(对象 发送器,鼠标按钮(Ventargs e) 在System.Windows.RoutedEventArgs.InvokeHandler(委托 处理程序(对象目标) 在System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象 路线(目标) 在System.Windows.EventRoute.InvokeHandlerSimp(对象 源,RoutedEventArgs参数,布尔值 (重播) 位于System.Windows.UIElement.RaiseEventImpl(DependencyObject 在System.Windows.UIElement.RaiseEvent处(RoutedEventArgs args,布尔值(可信) 在System.Windows.Input.InputManager.ProcessStaginArea()中 位于System.Windows.Input.InputManager.ProcessInput(InputEventArgs 输入) 位于System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd,InputMode,Int32时间戳, Int32 y,Int32车轮) lpram、Boolean&(已处理) 在System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lpram、Boolean&(已处理) lpram、Boolean&(已处理) 在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 回调,对象参数,布尔值 位于System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象 源、委托回调、对象 args,布尔参数, 在System.Windows.Threading.Dispatcher.WrappedInvoke(委托 回调,对象参数,布尔值 IsSingle参数,委托 捕手) 位于System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级、TimeSpan超时、委托 方法,对象参数,布尔值 IsSingle参数) 优先级、委托方法、对象参数) 在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr (内存) 味精) 位于System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame (框架) 位于System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame (框架) 在System.Windows.Threading.Dispatcher.Run()中 位于System.Windows.Application.RunDispatcher(对象 忽略) (窗口) (窗口) 在System.Windows.Application.Run()处 在中的WpfDatagridTest.App.Main()处 0 位于System.AppDomain.\u下一个安全组件(程序集 程序集,字符串[]args) 位于System.AppDomain.ExecuteAssembly(字符串 档案、证据 assemblySecurity,字符串[]args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上 在System.Threading.ThreadHelper.ThreadStart\u上下文(对象 (州) 在System.Threading.ExecutionContext.Run(ExecutionContext 回调(对象状态) 位于System.Threading.ThreadHelper.ThreadStart()处 内部异常:

    1 回复  |  直到 15 年前
        1
  •  3
  •   Charlie    15 年前

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

    此外,我相信还有一个问题 IsReadOnly 属性(检查DataGrid文档),该属性需要设置为false(但我认为默认情况下为false)。