当用户完成在WPF中的DataGrid
中的单元格编辑并尝试更新DataTable
以反映这一点时,我正在捕获。
但是,代码进入“中断模式”,然后才关闭。
private void DataGridView1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataGridColumn col1 = e.Column;
DataGridRow row1 = e.Row;
int row_index = ((System.Windows.Controls.DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
int col_index = col1.DisplayIndex;
var itemtext = ((System.Windows.Controls.TextBox)e.EditingElement).Text;
if(dt.Rows[row_index].ItemArray[col_index].ToString()!= itemtext)
{
DataGridView1.DataContext = null;
dt.Rows[row_index][col_index] = itemtext;
DataGridView1.DataContext = dt;
}
}
有人知道这是什么原因吗?
dt
是DataTable
。
每次编辑单元格时都要更新DataContext
的原因是,我有一段代码说明单元格值是否超过33个字符以使单元格变为红色,所以每当单元格被编辑时,我希望颜色更新以反映这一点。
更新
我查看了输出调试,发现了以下错误
PresentationCore.dll中发生了'System.ArgumentNullException'类型的未处理异常 值不能为空。
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: element
at
System.Windows.Automation.Peers.UIElementAutomationPeer.FromElement(UIElement element)
at System.Windows.Controls.DataGrid.CellAutomationValueHolder.TrackValue()
at System.Windows.Controls.DataGrid.UpdateCellAutomationValueHolder(DataGridCell cell)
at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(Object sender, ExecutedRoutedEventArgs e)
at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)
at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target)
at System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode)
at System.Windows.Controls.DataGrid.CommitAnyEdit()
at System.Windows.Controls.DataGrid.OnEnterKeyDown(KeyEventArgs e)
at System.Windows.Controls.DataGrid.OnKeyDown(KeyEventArgs e)
at System.Windows.UIElement.OnKeyDownThunk(Object sender, KeyEventArgs e)
at System.Windows.Input.KeyEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
at System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled)
at System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers)
at System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at System.Windows.Interop.HwndSource.OnPreprocessMessageThunk(MSG& msg, Boolean& handled)
at System.Windows.Interop.HwndSource.WeakEventPreprocessMessage.OnPreprocessMessage(MSG& msg, Boolean& handled)
at System.Windows.Interop.ThreadMessageEventHandler.Invoke(MSG& msg, Boolean& handled)
at System.Windows.Interop.ComponentDispatcherThread.RaiseThreadMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at DMHotFolderMain.App.Main()
我发现只有在DataContext
为空并且一起调用更新DataTable
时,这才崩溃。
我尝试注释掉这些行,它们是独立工作的,而不是一起工作的。
答案 0 :(得分:0)
我建议您使用ValidationRule
,因为它更容易处理。您可以根据用户输入提供自定义的视觉反馈(例如更改backgorund颜色)。
public class TextLengthValidationRule : ValidationRule
{
public override ValidationResult Validate(object value,
System.Globalization.CultureInfo cultureInfo)
{
var str = (value as BindingGroup).Items[0] as string;
if (str.Length > 33)
{
return new ValidationResult(false,
"Text must be shorter than 33 chars");
}
else
{
return ValidationResult.ValidResult;
}
}
}