记下这个使用 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 工具包数据网格,它有 10 列带有标题和一个空行。
选择空行的某个单元格,然后单击编辑时出现问题:程序崩溃。Visual Studio 中出现一个消息框,上面写着:“当前位置没有可用的源代码。” 我想知道如何使数据网格中的单元格可编辑?
这是异常和堆栈跟踪:
System.InvalidOperationException 未处理 Message="'EditItem' 不允许用于此视图。"
来源="演示框架"
StackTrace:在 System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) 在 Microsoft.Windows.Controls.DataGrid.EditRowItem(Object rowItem) 在 C:\dd\WPF_1\src\wpf\src\ 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:第 2208 行在 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 处的第 2036 行(对象发送者,ExecutedRoutedEventArgs e) 在 System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding) at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection 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) 在 System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target) 在 System.Windows.RoutedEventArgs。InvokeHandler(委托处理程序,对象目标)在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,RoutedEventArgs routedEventArgs)在 System.Windows.EventRoute.InvokeHandlersImpl(对象源,RoutedEventArgs 参数,布尔 reRaised)在 System.Windows.UIElement.RaiseEventImpl( DependencyObject 发送者,RoutedEventArgs 参数)在 System.Windows.UIElement.RaiseEvent(RoutedEventArgs 参数,布尔信任)在 System.Windows.Input.RoutedCommand.ExecuteImpl(对象参数,IInputElement 目标,布尔用户启动)在 System.Windows.Input.RoutedCommand。在 C 中的 Microsoft.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs editingEventArgs) 处执行(对象参数,IInputElement 目标):\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:C:\dd\WPF_1 中 Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e) 的第 3059 行\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGridCell.cs:C:\dd\WPF_1\ 中 Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(Object sender, MouseButtonEventArgs e) 的第 748 行src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGridCell.cs:System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler 的第 726 行(Delegate genericHandler,Object genericTarget)在 System.Windows.RoutedEventArgs.InvokeHandler( System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs) 在 System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 在 System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) 在 System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender) , MouseButtonEventArgs e) 在 System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e) 在 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 在 System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target ) 在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 在 System.Windows。System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trust) 中 System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 中的 EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)。 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 msg,IntPtr wParam,IntPtr lParam,Boolean&处理)在 System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理)在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&处理)在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback , 对象 args, Boolean isSingleParameter, Delegate catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) 在 System.Windows.Threading.Dispatcher。InvokeImpl(DispatcherPriority 优先级,TimeSpan 超时,Delegate 方法,Object args,Boolean isSingleParameter)在 System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority 优先级,Delegate 方法,Object arg)在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg, IntPtr wParam, IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 在 System .Windows.Threading.Dispatcher.Run() 在 System.Windows.Application.RunDispatcher(Object ignore) 在 System.Windows.Application.RunInternal(Window window) 在 System.Windows.Application.Run(Window window) 在 System。Windows.Application.Run() 在 WpfDatagridTest.App.Main() 中 C:\Users\Frank\Documents\Visual_Studio_2008\Projects\WpfDatagridTest\WpfDatagridTest\obj\Debug\App.g.cs: System.AppDomain 的第 0 行。 _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状态)在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态)在 System.Threading.ThreadHelper.ThreadStart() InnerException:\Users\Frank\Documents\Visual_Studio_2008\Projects\WpfDatagridTest\WpfDatagridTest\obj\Debug\App.g.cs:System.AppDomain._nExecuteAssembly 的第 0 行(Assembly 程序集,String[] args)在 System.AppDomain.ExecuteAssembly(String assemblyFile, 证据 assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object状态)在 System.Threading.ThreadHelper.ThreadStart() InnerException:\Users\Frank\Documents\Visual_Studio_2008\Projects\WpfDatagridTest\WpfDatagridTest\obj\Debug\App.g.cs:System.AppDomain._nExecuteAssembly 的第 0 行(Assembly 程序集,String[] args)在 System.AppDomain.ExecuteAssembly(String assemblyFile, 证据 assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object状态)在 System.Threading.ThreadHelper.ThreadStart() InnerException: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回调,对象状态)在 System.Threading.ThreadHelper.ThreadStart() InnerException: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回调,对象状态)在 System.Threading.ThreadHelper.ThreadStart() InnerException: