C # WPF toolkit: come posso rendere modificabile una cella da un datagrid?
-
22-07-2019 - |
Domanda
Prendi nota del codice per questo piccolo programma C # WPF realizzato con Microsoft Visual Studio 2008:
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("");
}
}
}
Questo programma mostra semplicemente un datagrid del toolkit WPF con 10 colonne con intestazioni e una riga vuota.
Si verifica un problema quando si seleziona una determinata cella della riga vuota e si fa clic per modificarla: il programma si arresta in modo anomalo. In Visual Studio viene visualizzata una finestra di messaggio che dice: " Non esiste un codice sorgente disponibile per la posizione corrente. & Quot; Vorrei sapere come posso rendere modificabile una cella di un datagrid?
Ecco l'eccezione e stacktrace:
System.InvalidOperationException era messaggio non gestito = " 'EditItem' non lo è consentito per questa vista. "
Source = " PresentationFramework "
StackTrace: at System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem (Object articolo) at Microsoft.Windows.Controls.DataGrid.EditRowItem (Object rowItem) in C: \ dd \ WPF_1 \ src \ WPF \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs: linea 3396 at Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit (ExecutedRoutedEventArgs e) in C: \ dd \ WPF_1 \ src \ WPF \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs: linea 2208 at Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit (Object mittente, ExecutedRoutedEventArgs e) in C: \ dd \ WPF_1 \ src \ WPF \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs: linea 2036 at System.Windows.Input.CommandBinding.OnExecuted (Object mittente, ExecutedRoutedEventArgs e) at System.Windows.Input.CommandManager.ExecuteCommandBinding (Object mittente, ExecutedRoutedEventArgs e, CommandBinding commandBinding) at System.Windows.Input.CommandManager.FindCommandBinding (CommandBindingCollection commandBindings, mittente oggetto, RoutedEventArgs e, comando ICommand, Esecuzione booleana) at System.Windows.Input.CommandManager.FindCommandBinding (Object mittente, RoutedEventArgs e, ICommand comando, booleano eseguito) at System.Windows.Input.CommandManager.OnExecuted (Object mittente, ExecutedRoutedEventArgs e) at System.Windows.UIElement.OnExecutedThunk (Object mittente, ExecutedRoutedEventArgs e) at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler (Delegate genericHandler, Target object) at System.Windows.RoutedEventArgs.InvokeHandler (Delegato gestore, oggetto target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler (Object target, RoutedEventArgs RoutedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl (Object source, args di RoutedEventArgs, booleano rilanciato) at System.Windows.UIElement.RaiseEventImpl (DependencyObject mittente, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent (RoutedEventArgs args, fidato booleano) at System.Windows.Input.RoutedCommand.ExecuteImpl (Object parametro, IInputElement target, Utente booleano Iniziato) at System.Windows.Input.RoutedCommand.Execute (Object parametro, IInputElement target) presso Microsoft.Windows.Controls.DataGrid.BeginEdit (RoutedEventArgs editingEventArgs) in C: \ dd \ WPF_1 \ src \ WPF \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs: linea 3059 at Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown (MouseButtonEventArgs e) in C: \ dd \ WPF_1 \ src \ WPF \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGridCell.cs: linea 748 at Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk (Object mittente, MouseButtonEventArgs e) in C: \ dd \ WPF_1 \ src \ WPF \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGridCell.cs: line 726 at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler (Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler (Delegato gestore, oggetto target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler (Object target, RoutedEventArgs RoutedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl (Object source, args di RoutedEventArgs, booleano rilanciato) at System.Windows.UIElement.ReRaiseEventAs (DependencyObject mittente, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent (DependencyObject mittente, MouseButtonEventArgs e) at System.Windows.UIElement.OnMouseDownThunk (Object mittente, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler (Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler (Delegato gestore, oggetto target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler (Object target, RoutedEventArgs RoutedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl (Object source, args di RoutedEventArgs, booleano rilanciato) at System.Windows.UIElement.RaiseEventImpl (DependencyObject mittente, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent (RoutedEventArgs args, fidato booleano) at System.Windows.Input.InputManager.ProcessStagingArea () at System.Windows.Input.InputManager.ProcessInput (InputEventArgs ingresso) at System.Windows.Input.InputProviderSite.ReportInput (InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput (IntPtr hwnd, modalità InputMode, timestamp Int32, RawMouseAzioni di azioni, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean & amp; gestito) at System.Windows.Interop.HwndSource.InputFilterMessage (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean & amp; gestito) at MS.Win32.HwndWrapper.WndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean & amp; gestito) at MS.Win32.HwndSubclass.DispatcherCallbackOperation (Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall (Delegate callback, Object args, Boolean isSingleParameter) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen (Object sorgente, callback delegato, oggetto args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke (Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl (DispatcherPriority priorità, timeout del TimeSpan, delegato metodo, Object args, booleano isSingleParameter) su System.Windows.Threading.Dispatcher.Invoke (DispatcherPriority priorità, metodo delegato, oggetto arg) at MS.Win32.HwndSubclass.SubclassWndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage (MSG & amp; msg) at System.Windows.Threading.Dispatcher.PushFrameImpl (DispatcherFrame telaio) at System.Windows.Threading.Dispatcher.PushFrame (DispatcherFrame telaio) at System.Windows.Threading.Dispatcher.Run () at System.Windows.Application.RunDispatcher (Object ignorare) at System.Windows.Application.RunInternal (Window finestra) at System.Windows.Application.Run (Window finestra) at System.Windows.Application.Run () presso WpfDatagridTest.App.Main () in C: \ Users \ Frank \ Documenti \ Visual_Studio_2008 \ Projects \ WpfDatagridTest \ WpfDatagridTest \ obj \ Debug \ App.g.cs: linea 0 a S
Soluzione
Stai impostando ItemsSource su Binding , ma non c'è DataContext, quindi questa associazione è inutile. Quindi stai generando le colonne nel code-behind e le riempi con dati temporanei. Da quanto ho capito su DataGrid, la modifica è supportata solo se ci sono dati a cui è legata la griglia. Altrimenti, quali dati stai modificando?
Inoltre, credo che esista una proprietà IsReadOnly (controlla la documentazione di DataGrid), che deve essere impostata su false (ma penso che sia falsa per impostazione predefinita).