C#WPFツールキット:データグリッドのセルを編集可能にするにはどうすればよいですか?
-
22-07-2019 - |
質問
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に次のようなメッセージボックスが表示されます:&quot;現在の場所に利用可能なソースコードがありません。&quot;データグリッドからセルを編集可能にする方法を知りたいですか?
ここに例外とスタックトレースがあります:
System.InvalidOperationExceptionは 未処理のMessage =&quot;「EditItem」は このビューで許可されています。&quot;
Source =&quot; PresentationFramework&quot;
スタックトレース: System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Objectで 項目) Microsoft.Windows.Controls.DataGrid.EditRowItem(Object rowItem)in 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(Objectで 送信者、ExecutedRoutedEventArgs e)で C:\ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs:line 2036 System.Windows.Input.CommandBinding.OnExecuted(Objectで 送信者、ExecutedRoutedEventArgs e) System.Windows.Input.CommandManager.ExecuteCommandBinding(Objectで 送信者、ExecutedRoutedEventArgs e、 CommandBinding commandBinding) System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollectionで commandBindings、オブジェクト送信者、 RoutedEventArgs e、ICommandコマンド、 ブール実行) System.Windows.Input.CommandManager.FindCommandBinding(Objectで 送信者、RoutedEventArgs e、ICommand コマンド、ブール実行) System.Windows.Input.CommandManager.OnExecuted(Objectで 送信者、ExecutedRoutedEventArgs e) System.Windows.UIElement.OnExecutedThunk(Object 送信者、ExecutedRoutedEventArgs e) System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegateで genericHandler、オブジェクトターゲット) System.Windows.RoutedEventArgs.InvokeHandler(Delegateで ハンドラー、オブジェクトターゲット) System.Windows.RoutedEventHandlerInfo.InvokeHandler(Objectで ターゲット、RoutedEventArgs routedEventArgs) System.Windows.EventRoute.InvokeHandlersImpl(Object source、RoutedEventArgs args、ブール値 再発生) System.Windows.UIElement.RaiseEventImpl(DependencyObjectで 送信者、RoutedEventArgs args) System.Windows.UIElement.RaiseEvent(RoutedEventArgsで args、ブール信頼済み) System.Windows.Input.RoutedCommand.ExecuteImpl(Object パラメーター、IInputElementターゲット、 ブールuserInitiated) System.Windows.Input.RoutedCommand.Execute(Objectで パラメーター、IInputElementターゲット) Microsoft.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgsで EditingEventArgs)in 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(Object 送信者、MouseButtonEventArgs e)で C:\ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGridCell.cs:line 726 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegateで genericHandler、Object genericTarget) System.Windows.RoutedEventArgs.InvokeHandler(Delegateで ハンドラー、オブジェクトターゲット) System.Windows.RoutedEventHandlerInfo.InvokeHandler(Objectで ターゲット、RoutedEventArgs routedEventArgs) System.Windows.EventRoute.InvokeHandlersImpl(Object source、RoutedEventArgs args、ブール値 再発生) System.Windows.UIElement.ReRaiseEventAs(DependencyObjectで 送信者、RoutedEventArgs args、 RoutedEvent newEvent) System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObjectで 送信者、MouseButtonEventArgs e) System.Windows.UIElement.OnMouseDownThunk(Object 送信者、MouseButtonEventArgs e) System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegateで genericHandler、Object genericTarget) System.Windows.RoutedEventArgs.InvokeHandler(Delegateで ハンドラー、オブジェクトターゲット) System.Windows.RoutedEventHandlerInfo.InvokeHandler(Objectで ターゲット、RoutedEventArgs routedEventArgs) System.Windows.EventRoute.InvokeHandlersImpl(Object source、RoutedEventArgs args、ブール値 再発生) System.Windows.UIElement.RaiseEventImpl(DependencyObjectで 送信者、RoutedEventArgs args) System.Windows.UIElement.RaiseEvent(RoutedEventArgsで args、ブール信頼済み) 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モード、Int32タイムスタンプ、 RawMouseActionsアクション、Int32 x、 Int32 y、Int32ホイール) System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtrで hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、ブール値&amp;取り扱い) System.Windows.Interop.HwndSource.InputFilterMessage(IntPtrで hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、ブール値&amp;取り扱い) MS.Win32.HwndWrapper.WndProc(IntPtrで hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、ブール値&amp;取り扱い) MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegateで コールバック、オブジェクト引数、ブール isSingleParameter) System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Objectで ソース、デリゲートコールバック、オブジェクト args、ブールisSingleParameter、 デリゲートcatchHandler) System.Windows.Threading.Dispatcher.WrappedInvoke(Delegateで コールバック、オブジェクト引数、ブール isSingleParameter、デリゲート catchHandler) System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriorityで 優先度、TimeSpanタイムアウト、デリゲート メソッド、オブジェクト引数、ブール isSingleParameter) System.Windows.Threading.Dispatcher.Invoke(DispatcherPriorityで 優先度、デリゲートメソッド、オブジェクト引数) MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtrで lParam) MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp;で msg) System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrameで フレーム) System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrameで フレーム) System.Windows.Threading.Dispatcher.Run()で System.Windows.Application.RunDispatcher(Object 無視) System.Windows.Application.RunInternal(Windowで 窓) System.Windows.Application.Run(Windowで 窓) System.Windows.Application.Run()で WpfDatagridTest.App.Main()で C:\ Users \ Frank \ Documents \ Visual_Studio_2008 \ Projects \ WpfDatagridTest \ WpfDatagridTest \ obj \ Debug \ App.g.cs:line 0 南で
解決
ItemsSourceを Binding に設定していますが、DataContextがないため、このバインディングは役に立ちません。次に、コードビハインドで列を生成し、一時データを入力します。私がDataGridについて理解していることから、編集はグリッドがバインドされているデータがある場合にのみサポートされます。それ以外の場合、どのデータを編集していますか?
さらに、 IsReadOnly プロパティ(DataGridのドキュメントを参照)があると思います。これはfalseに設定する必要があります(ただし、デフォルトではfalseと考えられます)。