【发布时间】:2010-11-16 20:16:51
【问题描述】:
记下这个使用 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' 不是 允许此视图。”
Source="PresentationFramework"
堆栈跟踪: 在 System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(对象 物品) 在 Microsoft.Windows.Controls.DataGrid.EditRowItem(对象 行项目)在 C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 3396 在 Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e) 在 C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 2208 在 Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit(对象 发件人,ExecutedRoutedEventArgs e) 在 C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 2036 在 System.Windows.Input.CommandBinding.OnExecuted(对象 发件人,ExecutedRoutedEventArgs e) 在 System.Windows.Input.CommandManager.ExecuteCommandBinding(对象 发件人,ExecutedRoutedEventArgs e, 命令绑定命令绑定) 在 System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings,对象发送者, 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 路由事件参数) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象 源,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 目标) 在 Microsoft.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs 编辑事件参数)在 C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGrid.cs:line 3059 在 Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e) 在 C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGridCell.cs:line 748 在 Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(对象 发件人,MouseButtonEventArgs e) 在 C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGridCell.cs:line 726 在 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(委托 genericHandler, 对象 genericTarget) 在 System.Windows.RoutedEventArgs.InvokeHandler(委托 处理程序,对象目标) 在 System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象 目标,RoutedEventArgs 路由事件参数) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象 源,RoutedEventArgs 参数,布尔值 重新提出) 在 System.Windows.UIElement.ReRaiseEventAs(DependencyObject 发件人,RoutedEventArgs 参数, RoutedEvent 新事件) 在 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 路由事件参数) 在 System.Windows.EventRoute.InvokeHandlersImpl(对象 源,RoutedEventArgs 参数,布尔值 重新提出) 在 System.Windows.UIElement.RaiseEventImpl(DependencyObject 发件人,RoutedEventArgs 参数) 在 System.Windows.UIElement.RaiseEvent(RoutedEventArgs args,布尔值信任) 在 System.Windows.Input.InputManager.ProcessStagingArea() 在 System.Windows.Input.InputManager.ProcessInput(InputEventArgs 输入) 在 System.Windows.Input.InputProviderSite.ReportInput(InputReport 输入报告) 在 System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode 模式, Int32 时间戳, RawMouseActions 动作,Int32 x, Int32 y,Int32 轮) 在 System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd,Int32 消息,IntPtr wParam,IntPtr lParam, Boolean & 处理) 在 System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 消息,IntPtr wParam,IntPtr lParam, Boolean & 处理) 在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 消息,IntPtr wParam,IntPtr lParam, Boolean & 处理) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调,对象参数,布尔值 isSingleParameter) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象 源,委托回调,对象 args,布尔 isSingleParameter, 委托 catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(委托 回调,对象参数,布尔值 isSingleParameter,委托 捕获处理程序) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级、TimeSpan 超时、委托 方法,对象参数,布尔值 isSingleParameter) 在 System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority 优先级、委托方法、对象 arg) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 味精,IntPtr wParam,IntPtr l 参数) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& 味精) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架) 在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame 框架) 在 System.Windows.Threading.Dispatcher.Run() 在 System.Windows.Application.RunDispatcher(对象 忽视) 在 System.Windows.Application.RunInternal(窗口 窗户) 在 System.Windows.Application.Run(窗口 窗户) 在 System.Windows.Application.Run() 在 WpfDatagridTest.App.Main() 中 C:\Users\Frank\Documents\Visual_Studio_2008\Projects\WpfDatagridTest\WpfDatagridTest\obj\Debug\App.g.cs:line 0 在 System.AppDomain._nExecuteAssembly(程序集 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 汇编文件,证据 装配安全,字符串 [] 参数) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象 状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext 执行上下文,上下文回调 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:
【问题讨论】:
-
1.3 k 观看次数,没有人赞成大声笑:D
标签: c# wpf datagrid wpftoolkit cell