WPF C# 文本框 IDataErrorInfo 错误 - 在 IsEnabled = true 时单击多次会擦除文本

WPF C# Textbox IDataErrorInfo Bug - Clicking multiple times, when IsEnabled = true erases text

我创建了一个绑定到实现 IDataErrorInfo 的实体的 wpf 文本框。当文本框无效时(它周围的红色框因为它未通过 IDataErrorInfo 验证)并且文本框的 IsEnabled 属性 设置为 false,我可以多次单击文本框并删除文本框内的文本(string.Empty 被传递给实体)。

这是怎么回事?这是预期的行为吗?

编辑:我删除了 isEnabled 并添加了 isreadonly 和 focusable。我意识到如果我没有点击文本框,如果验证失败它就会被删除。

编辑:然后,我删除了 focusable 并再次尝试。这一次,在我右击代表验证的红线之前,没有任何内容被删除。

编辑:我的文本框位于 DataGridTemplateColumn 内 DataGridTemplateColumn.CellTemplate 内的 DataTemplate 内。

调用栈如下:

MyCode.DLL!MyCode.Models.Entity.PropertyName.set(string value) Line 66 C# [Native to Managed Transition]
PresentationFramework.dll!MS.Internal.Data.PropertyPathWorker.SetValue(object item, object value) Unknown PresentationFramework.dll!MS.Internal.Data.ClrBindingWorker.UpdateValue(object value) Unknown PresentationFramework.dll!System.Windows.Data.BindingExpression.UpdateSource(object value) Unknown PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.UpdateValue() Unknown PresentationFramework.dll!System.Windows.Data.BindingExpression.UpdateOverride() Unknown PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.Update() Unknown PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.ProcessDirty() Unknown PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.Dirty() Unknown PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.SetValue(System.Windows.DependencyObject d, System.Windows.DependencyProperty dp, object value) Unknown WindowsBase.dll!System.Windows.DependencyObject.SetValueCommon(System.Windows.DependencyProperty dp, object value, System.Windows.PropertyMetadata metadata, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType, bool isInternal) Unknown WindowsBase.dll!System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty dp, object value) Unknown PresentationFramework.dll!System.Windows.Data.BindingExpression.GetInitialValue(System.Windows.DependencyObject target, out System.Windows.Controls.ValidationError error) Unknown PresentationFramework.dll!System.Windows.Data.BindingExpression.Activate(object item) Unknown PresentationFramework.dll!System.Windows.Data.BindingExpression.AttachToContext(System.Windows.Data.BindingExpression.AttachAttempt attempt) Unknown PresentationFramework.dll!System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(bool lastChance) Unknown PresentationFramework.dll!MS.Internal.Data.DataBindEngine.Task.Run(bool lastChance) Unknown PresentationFramework.dll!MS.Internal.Data.DataBindEngine.Run(object arg) Unknown PresentationFramework.dll!MS.Internal.Data.DataBindEngine.OnLayoutUpdated(object sender, System.EventArgs e) Unknown PresentationCore.dll!System.Windows.ContextLayoutManager.fireLayoutUpdateEvent() Unknown PresentationCore.dll!System.Windows.ContextLayoutManager.UpdateLayout() Unknown PresentationCore.dll!System.Windows.UIElement.UpdateLayout() Unknown PresentationFramework.dll!System.Windows.Controls.DataGridCell.BeginEdit(System.Windows.RoutedEventArgs e) Unknown PresentationFramework.dll!System.Windows.Controls.DataGrid.OnExecutedBeginEdit(System.Windows.Input.ExecutedRoutedEventArgs e) Unknown PresentationFramework.dll!System.Windows.Controls.DataGrid.OnExecutedBeginEdit(object sender, System.Windows.Input.ExecutedRoutedEventArgs e) Unknown PresentationCore.dll!System.Windows.Input.CommandBinding.OnExecuted(object sender, System.Windows.Input.ExecutedRoutedEventArgs e) Unknown PresentationCore.dll!System.Windows.Input.CommandManager.ExecuteCommandBinding(object sender, System.Windows.Input.ExecutedRoutedEventArgs e, System.Windows.Input.CommandBinding commandBinding) Unknown PresentationCore.dll!System.Windows.Input.CommandManager.FindCommandBinding(object sender, System.Windows.RoutedEventArgs e, System.Windows.Input.ICommand command, bool execute) Unknown PresentationCore.dll!System.Windows.Input.CommandManager.OnExecuted(object sender, System.Windows.Input.ExecutedRoutedEventArgs e) Unknown PresentationCore.dll!System.Windows.UIElement.OnExecutedThunk(object sender, System.Windows.Input.ExecutedRoutedEventArgs e) Unknown PresentationCore.dll!System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(System.Delegate genericHandler, object target) Unknown PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Unknown PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) Unknown PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Unknown PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) Unknown PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args, bool trusted) Unknown PresentationCore.dll!System.Windows.Input.RoutedCommand.ExecuteImpl(object parameter, System.Windows.IInputElement target, bool userInitiated) Unknown PresentationCore.dll!System.Windows.Input.RoutedCommand.Execute(object parameter, System.Windows.IInputElement target) Unknown PresentationFramework.dll!System.Windows.Controls.DataGrid.BeginEdit(System.Windows.RoutedEventArgs editingEventArgs) Unknown PresentationFramework.dll!System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e) Unknown PresentationFramework.dll!System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(object sender, System.Windows.Input.MouseButtonEventArgs e) Unknown PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate genericHandler, object genericTarget) Unknown PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Unknown PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) Unknown PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Unknown PresentationCore.dll!System.Windows.UIElement.ReRaiseEventAs(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args, System.Windows.RoutedEvent newEvent) Unknown PresentationCore.dll!System.Windows.UIElement.OnMouseDownThunk(object sender, System.Windows.Input.MouseButtonEventArgs e) Unknown PresentationCore.dll!System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(System.Delegate genericHandler, object genericTarget) Unknown PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) Unknown PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) Unknown PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Unknown PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) Unknown PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args) Unknown PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs args, bool trusted) Unknown PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() Unknown PresentationCore.dll!System.Windows.Input.InputManager.ProcessInput(System.Windows.Input.InputEventArgs input) Unknown PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport) Unknown PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(System.IntPtr hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawMouseActions actions, int x, int y, int wheel) Unknown PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(System.IntPtr hwnd, MS.Internal.Interop.WindowMessage msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Unknown WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Unknown [AppDomain Transition]

我一直不明白为什么,但我发现使用 CellTemplateCellEditingTemplate 解决了我的问题。

对于那些对我的 post 和 运行 投反对票的人,如果您无法理解问题的复杂性,我建议您以后忽略这个问题。