簡體   English   中英

C#WPF工具包:如何​​使數據網格中的單元格可編輯?

[英]C# WPF toolkit: How can I make a cell from a datagrid be editable?

記下使用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工具包datagrid,其中包含10個帶標題和空行的列。

當選擇空行的某個單元格然后單擊進行編輯時會出現問題:程序崩潰。 Visual Studio中顯示一個消息框,上面寫着:“當前位置沒有可用的源代碼。” 我想知道如何使數據網格中的單元格可編輯?


這是異常和堆棧跟蹤:

System.InvalidOperationException未處理Message =“'EditItem'不允許此視圖。”
來源= “PresentationFramework”
StackTrace:位於C:\\ dd \\ WPF_1 \\ src \\ wpf \\ src \\中的Microsoft.Windows.Controls.DataGrid.EditRowItem(Object rowItem)中的System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.EditItem(Object item) 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(Object sender,ExecutedRoutedEventArgs e)中的第2036行,位於System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender,ExecutedRoutedEventArgs e,CommandBinding commandBinding)在System.Windows.Input.CommandManager.FindCommandBinding(CommandBindin System.Windows.Input.CommandManager.OnExecuted(Object)上的System.Windows.Input.CommandManager.FindCommandBinding(Object sender,RoutedEventArgs,ICommand命令,布爾執行)中的gCollection commandBindings,Object sender,RoutedEventArgs e,ICommand命令,布爾執行)發送者,ExecutedRoutedEventArgs e)在System.Windows.RindtedEventArgs.InvokeHandler(委托處理程序,對象)的System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler,Object target)中的System.Windows.UIElement.OnExecutedThunk(Object sender,ExecutedRoutedEventArgs e)處。在System.Windows.Uindlement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)的System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)上的System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)中的目標) System.Windows.Input.RoutedComma上的System.Windows.UIElement.RaiseEvent(RoutedEventArgs args,布爾值信任) 在C:\\ dd \\中的Microsoft.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs editingEventArgs)的System.Windows.Input.RoutedCommand.Execute(Object parameter,IInputElement target)中的nd.ExecuteImpl(Object parameter,IInputElement target,Boolean userInitiated) WPF_1 \\ src \\ wpf \\ src \\ ControlsPack \\ WPFToolkit \\ DataGrid \\ Microsoft \\ Windows \\ Controls \\ DataGrid.cs:C:\\ dd \\ WPF_1 \\ src \\中Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e)中的第3059行wpf \\ src \\ ControlsPack \\ WPFToolkit \\ DataGrid \\ Microsoft \\ Windows \\ Controls \\ DataGridCell.cs:位於C:\\ dd \\ WPF_1 \\ src \\ wpf中的Microsoft.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(Object sender,MouseButtonEventArgs e)中的第748行\\ SRC \\ ControlsPack \\ WPFToolkit \\ DataGrid中\\微軟\\的Windows \\控制\\ DataGridCell.cs:行726 System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(代表genericHandler,對象genericTarget)在System.Windows.RoutedEventArgs.InvokeHandler(委托處理程序, System.Windows.Routed上的對象目標 System.Windows上的System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender,RoutedEventArgs args,RoutedEvent newEvent)上的System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)中的EventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)位於System.Windows.RindtedEventArgs的System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler,Object genericTarget)的System.Windows.UIElement.OnMouseDownThunk(Object sender,MouseButtonEventArgs e)中的.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender,MouseButtonEventArgs e)。位於System.Windows.Uindlement.RaiseEventImpl的System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)的System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)中的InvokeHandler(委托處理程序,Object目標)( System.Windows.UI上的DependencyObject sender,RoutedEventArgs args) System.Windows.Input.InputManager.ProcessInput中的System.Windows.InputManager.ProcessStagingArea()處於System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)的System.RaiseEvent(RoutedEventArgs args,布爾值受信任)在System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr的HWND,INPUTMODE模式,的Int32時間戳,RawMouseActions動作的Int32的x,的Int32 Y,的Int32輪)在System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr的HWND,MSG的Int32,IntPtr的位於MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr)的System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling)中的wParam,IntPtr lParam,Boolean&handling) System.Windows.T上System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回調,Object args,Boolean isSingleParameter)的MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)中的wParam,IntPtr lParam,Boolean&handling) hreading.ExceptionWrapper.TryCatchWhen在System.Windows.Threading.Dispatcher.WrappedInvoke(對象源,代表回調,對象指定參數時,布爾isSingleParameter,代表catchHandler)(代表回調,對象指定參數時,布爾isSingleParameter,代表catchHandler)在System.Windows.Threading程序.Dispatcher.InvokeImpl(的DispatcherPriority優先權,時間跨度超時,委托方法,對象指定參數時,布爾isSingleParameter)在System.Windows.Threading.Dispatcher.Invoke(的DispatcherPriority優先權,委托方法,對象ARG)在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr的在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架)的System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)的MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg)中的hwnd,Int32消息,IntPtr wParam,IntPtr lParam) )在系統的System.Windows.Application.RunInternal(窗口窗口)的System.Windows.Application.RunDispatcher(Object ignore)處的System.Windows.Threading.Dispatcher.Run()處 System.Windows.Application.Run()中的.Windows.Application.Run(窗口窗口)位於C:\\ Users \\ Frank \\ Documents \\ Visual_Studio_2008 \\ Projects \\ WpfDatagridTest \\ WpfDatagridTest \\ _ obj \\ Debug \\中的WpfDatagridTest.App.Main() App.g.cs:System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)的第0行,位於Microsoft.VisualStudio.HostingProcess.HostProc的System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)。在System.Threading.Thread.ThreadStart()的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回調,對象狀態)的System.Threading.ThreadHelper.ThreadStart_Context(Object state)中的RunUsersAssembly()InnerException:

您將ItemsSource設置為Binding ,但沒有DataContext,因此此綁定無用。 然后,您將在代碼隱藏中生成列並使用臨時數據填充它們。 根據我對DataGrid的理解,只有在網格綁定的數據時才支持編輯。 否則,您正在編輯哪些數據?

此外,我相信有一個IsReadOnly屬性(檢查DataGrid文檔),需要將其設置為false(但我認為默認情況下它是false)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM