Boîte à outils C # WPF: Comment puis-je modifier une cellule d’une grille de données?
-
22-07-2019 - |
Question
Prenez note du code de ce petit programme WPF C # créé avec 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("");
}
}
}
Ce programme affiche simplement une grille de données WPF Toolkit avec 10 colonnes avec en-têtes et une ligne vide.
Il y a un problème qui se produit lorsqu'une certaine cellule de la ligne vide est sélectionnée puis cliquée pour être modifiée: le programme se bloque. Dans Visual Studio, un message s'affiche: "Il n'y a pas de code source disponible pour l'emplacement actuel". J'aimerais savoir comment puis-je modifier une cellule d'une grille de données?
Voici l'exception et stacktrace:
System.InvalidOperationException était Message non géré = "EditItem" n'est pas autorisé pour cette vue. "
Source = " PresentationFramework "
Trace de la pile: à System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem (Object article) à Microsoft.Windows.Controls.DataGrid.EditRowItem (Object rowItem) dans C: \ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs: ligne 3396 à Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit (ExecutedRoutedEventArgs e) dans C: \ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs: ligne 2208 à Microsoft.Windows.Controls.DataGrid.OnExecutedBeginEdit (Object expéditeur, ExecutedRoutedEventArgs e) dans C: \ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs: ligne 2036 à System.Windows.Input.CommandBinding.OnExecuted (Object expéditeur, ExecutedRoutedEventArgs e) à System.Windows.Input.CommandManager.ExecuteCommandBinding (Object expéditeur, ExecutedRoutedEventArgs e, CommandBinding commandBinding) sur System.Windows.Input.CommandManager.FindCommandBinding (CommandBindingCollection commandBindings, expéditeur d'objet, RoutedEventArgs e, commande ICommand, Exécution booléenne) à System.Windows.Input.CommandManager.FindCommandBinding (Object expéditeur, RoutedEventArgs e, ICommand commande, Boolean execute) à System.Windows.Input.CommandManager.OnExecuted (Object expéditeur, ExecutedRoutedEventArgs e) à System.Windows.UIElement.OnExecutedThunk (Object expéditeur, ExecutedRoutedEventArgs e) à System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler (Delegate genericHandler, Object target) à System.Windows.RoutedEventArgs.InvokeHandler (Delegate gestionnaire, cible d'objet) à System.Windows.RoutedEventHandlerInfo.InvokeHandler (Object cible, RoutedEventArgs routedEventArgs) à System.Windows.EventRoute.InvokeHandlersImpl (Object source, RoutedEventArgs args, Boolean relancé) à System.Windows.UIElement.RaiseEventImpl (DependencyObject expéditeur, arguments RoutedEventArgs) à System.Windows.UIElement.RaiseEvent (RoutedEventArgs args, booléen de confiance) à System.Windows.Input.RoutedCommand.ExecuteImpl (Object paramètre, cible IInputElement, Boolean userInitiated) à System.Windows.Input.RoutedCommand.Execute (Object paramètre, cible IInputElement) à Microsoft.Windows.Controls.DataGrid.BeginEdit (RoutedEventArgs montageEventArgs) dans C: \ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGrid.cs: ligne 3059 à Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown (MouseButtonEventArgs e) dans C: \ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ DataGridCell.cs: line 748 à Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk (Object expéditeur, MouseButtonEventArgs e) dans 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 gestionnaire, cible d'objet) à System.Windows.RoutedEventHandlerInfo.InvokeHandler (Object cible, RoutedEventArgs routedEventArgs) à System.Windows.EventRoute.InvokeHandlersImpl (Object source, RoutedEventArgs args, Boolean relancé) à System.Windows.UIElement.ReRaiseEventAs (DependencyObject expéditeur, arguments RoutedEventArgs, RoutedEvent newEvent) à System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent (DependencyObject expéditeur, MouseButtonEventArgs e) à System.Windows.UIElement.OnMouseDownThunk (Object expéditeur, MouseButtonEventArgs e) à System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler (Delegate genericHandler, Object genericTarget) à System.Windows.RoutedEventArgs.InvokeHandler (Delegate gestionnaire, cible d'objet) à System.Windows.RoutedEventHandlerInfo.InvokeHandler (Object cible, RoutedEventArgs routedEventArgs) à System.Windows.EventRoute.InvokeHandlersImpl (Object source, RoutedEventArgs args, Boolean relancé) à System.Windows.UIElement.RaiseEventImpl (DependencyObject expéditeur, arguments RoutedEventArgs) à System.Windows.UIElement.RaiseEvent (RoutedEventArgs args, booléen de confiance) à System.Windows.Input.InputManager.ProcessStagingArea () à System.Windows.Input.InputManager.ProcessInput (InputEventArgs contribution) à System.Windows.Input.InputProviderSite.ReportInput (InputReport inputReport) sur System.Windows.Interop.HwndMouseInputProvider.ReportInput (IntPtr hwnd, mode InputMode, horodatage Int32, Actions RawMouseActions, Int32 x, Int32 y, roue Int32) à System.Windows.Interop.HwndMouseInputProvider.FilterMessage (IntPtr msg, IntPtr wParam, IntPtr lParam, Boolean & amp; traité) sur System.Windows.Interop.HwndSource.InputFilterMessage (IntPtr msg, IntPtr wParam, IntPtr lParam, Boolean & amp; traité) à MS.Win32.HwndWrapper.WndProc (IntPtr msg, IntPtr wParam, IntPtr lParam, Boolean & amp; traité) at MS.Win32.HwndSubclass.DispatcherCallbackOperation (Object o) à System.Windows.Threading.ExceptionWrapper.InternalRealCall (Delegate callback, object args, booléen isSingleParameter) à System.Windows.Threading.ExceptionWrapper.TryCatchWhen (Object source, déléguer le rappel, objet args, Boolean isSingleParameter, Déléguer catchHandler) sur System.Windows.Threading.Dispatcher.WrappedInvoke (Delegate callback, object args, booléen isSingleParameter, Delegate catchHandler) sur System.Windows.Threading.Dispatcher.InvokeImpl (DispatcherPriority priorité, délai d'attente TimeSpan, délégué méthode, Object args, Boolean isSingleParameter) sur System.Windows.Threading.Dispatcher.Invoke (DispatcherPriority priorité, méthode Delegate, Object arg) à l'adresse MS.Win32.HwndSubclass.SubclassWndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) à MS.Win32.UnsafeNativeMethods.DispatchMessage (MSG & amp; msg) sur System.Windows.Threading.Dispatcher.PushFrameImpl (DispatcherFrame Cadre) sur System.Windows.Threading.Dispatcher.PushFrame (DispatcherFrame Cadre) à System.Windows.Threading.Dispatcher.Run () à System.Windows.Application.RunDispatcher (Object ignorer) à System.Windows.Application.RunInternal (Window la fenêtre) à System.Windows.Application.Run (Window la fenêtre) à System.Windows.Application.Run () à WpfDatagridTest.App.Main () in C: \ Utilisateurs \ Frank \ Documents \ Visual_Studio_2008 \ Projets \ WpfDatagridTest \ WpfDatagridTest \ obj \ Debug \ App.g.cs: line 0 chez S
La solution
Vous définissez votre ItemsSource sur Liaison , mais il n'y a pas de DataContext, cette liaison est donc inutile. Ensuite, vous générez les colonnes dans le code-behind et les remplissez avec des données temporaires. D'après ce que je comprends de DataGrid, l'édition n'est prise en charge que s'il existe des données auxquelles la grille est liée. Sinon, quelles données modifiez-vous?
De plus, je pense qu'il existe une propriété IsReadOnly (consultez la documentation de DataGrid), qui doit être définie sur false (mais je pense que c'est faux par défaut).