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("");
}
}
}
このプログラムは、ヘッダー付きの 10 列と空の行を持つ WPF ツールキット データグリッドを表示するだけです。
空の行の特定のセルを選択してからクリックして編集すると、プログラムがクラッシュするという問題があります。Visual Studio に、「現在の場所で使用できるソース コードはありません」というメッセージ ボックスが表示されます。データグリッドのセルを編集可能にする方法を教えてください。
例外とスタックトレースは次のとおりです。
System.InvalidOperationException は処理されませんでした Message="'EditItem' はこのビューには許可されていません。"
ソース="プレゼンテーション フレームワーク"
StackTrace: C:\dd\WPF_1\src\wpf\src\ の System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) で Microsoft.Windows.Controls.DataGrid.EditRowItem(Object rowItem) で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 (オブジェクト送信者、ExecutedRoutedEventArgs e) at 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(オブジェクト センダー、RoutedEventArgs e、ICommand コマンド、ブール値の実行) System.Windows.Input.CommandManager.OnExecuted(オブジェクト センダー、ExecutedRoutedEventArgs e) で System.Windows.UIElement.OnExecutedThunk(オブジェクト センダー) 、System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler (デリゲート genericHandler、オブジェクト ターゲット) で System.Windows.RoutedEventArgs e) を実行します。System.Windows.RoutedEventHandlerInfo.InvokeHandler(オブジェクト ターゲット、RoutedEventArgs routedEventArgs) の InvokeHandler (デリゲート ハンドラー、オブジェクト ターゲット) System.Windows.EventRoute.InvokeHandlersImpl (オブジェクト ソース、RoutedEventArgs args、Boolean reRaised) で System.Windows.UIElement.RaiseEventImpl( System.Windows.Input.RoutedCommand.ExecuteImpl (オブジェクト パラメーター、IInputElement ターゲット、Boolean userInitiated) の System.Windows.UIElement.RaiseEvent(RoutedEventArgs args、Boolean trusted) の DependencyObject 送信者、RoutedEventArgs args)。 C:\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 (オブジェクト送信者、MouseButtonEventArgs e) の 748 行目src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\DataGridCell.cs:System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(デリゲートgenericHandler、オブジェクトgenericTarget)の726行目System.Windows.RoutedEventArgs.InvokeHandler( System.Windows.RoutedEventHandlerInfo.InvokeHandler(オブジェクト ターゲット、System.Windows.UIElement.ReRaiseEventAs(DependencyObject 送信者、RoutedEventArgs 引数、RoutedEvent newEvent) で 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(Object target, RoutedEventArgs routedEventArgs) で System.Windows.System.Windows.UIElement.RaiseEventImpl(DependencyObject 送信者、RoutedEventArgs args) の EventRoute.InvokeHandlersImpl (オブジェクト ソース、RoutedEventArgs args、Boolean reRaised) System.Windows.UIElement.RaiseEvent (RoutedEventArgs args、Boolean trusted) の System.Windows.Input. InputManager.ProcessStagingArea() で System.Windows.Input.InputManager.ProcessInput(InputEventArgs 入力) で System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) で System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode モード, System.Windows.Interop.HwndMouseInputProvider.FilterMessage(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) 、オブジェクト引数、ブール値 isSingleParameter、デリゲート catchHandler) を System.Windows.Threading.Dispatcher.WrappedInvoke で (デリゲート コールバック、オブジェクト引数、ブール値 isSingleParameter、デリゲート catchHandler) を System.Windows.Threading.Dispatcher で使用します。MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32) での System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority 優先度、デリゲート メソッド、オブジェクト引数) での InvokeImpl (DispatcherPriority 優先度、TimeSpan タイムアウト、デリゲート メソッド、オブジェクト引数、Boolean isSingleParameter) System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame フレーム) の MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame フレーム) の msg、IntPtr wParam、IntPtr lParam) .Windows.Threading.Dispatcher.Run() at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.System.AppDomain の C:\Users\Frank\Documents\Visual_Studio_2008\Projects\WpfDatagridTest\WpfDatagridTest\obj\Debug\App.g.cs:line 0 の WpfDatagridTest.App.Main() での Windows.Application.Run()。 System.AppDomain.ExecuteAssembly の _nExecuteAssembly (アセンブリ アセンブリ、String[] 引数) (Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() の 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 (アセンブリ アセンブリ、String[] 引数) System.AppDomain.ExecuteAssembly(String) でassemblyFile、Evidence assemblySecurity、String[] args) の Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() の System.Threading.ThreadHelper.ThreadStart_Context(オブジェクト状態) の 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 (アセンブリ アセンブリ、String[] 引数) System.AppDomain.ExecuteAssembly(String) でassemblyFile、Evidence assemblySecurity、String[] args) の Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() の System.Threading.ThreadHelper.ThreadStart_Context(オブジェクト状態) の System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback コールバック、オブジェクト状態) System.Threading.ThreadHelper.ThreadStart() で InnerException:Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() で ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String[] args) System.Threading.ThreadHelper.ThreadStart_Context(オブジェクト状態) で System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback)コールバック、オブジェクト状態) で System.Threading.ThreadHelper.ThreadStart() InnerException:Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() で ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String[] args) System.Threading.ThreadHelper.ThreadStart_Context(オブジェクト状態) で System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback)コールバック、オブジェクト状態) で System.Threading.ThreadHelper.ThreadStart() InnerException: