Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell WPF DataGrid:提交空行时引发异常_Wpf_Powershell_Datagrid_Wpftoolkit - Fatal编程技术网

PowerShell WPF DataGrid:提交空行时引发异常

PowerShell WPF DataGrid:提交空行时引发异常,wpf,powershell,datagrid,wpftoolkit,Wpf,Powershell,Datagrid,Wpftoolkit,我在尝试提交空DataGrid行时遇到异常 System.NullReferenceException: Object reference not set to an instance of an object. at MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid() at MS.Internal.Data.PropertyPathWorker.get_IsDBNullValidForUpdate(

我在尝试提交空DataGrid行时遇到异常

System.NullReferenceException: Object reference not set to an instance of an object.
   at MS.Internal.Data.PropertyPathWorker.DetermineWhetherDBNullIsValid()
   at MS.Internal.Data.PropertyPathWorker.get_IsDBNullValidForUpdate()
   at MS.Internal.Data.ClrBindingWorker.get_IsDBNullValidForUpdate()
   at System.Windows.Data.BindingExpression.ConvertProposedValue(Object value)
   at System.Windows.Data.BindingExpressionBase.UpdateValue()
   at System.Windows.Data.BindingExpression.Update(Boolean synchronous)
   at System.Windows.Data.BindingExpression.UpdateSource()
   at Microsoft.Windows.Controls.DataGridHelper.UpdateSource(FrameworkElement element, DependencyProperty dp)
   at Microsoft.Windows.Controls.DataGridTextColumn.CommitCellEdit(FrameworkElement editingElement)
   at Microsoft.Windows.Controls.DataGridColumn.CommitEdit(FrameworkElement editingElement)
   at Microsoft.Windows.Controls.DataGridCell.CommitEdit()
   at Microsoft.Windows.Controls.DataGrid.OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
   at Microsoft.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(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
   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 Microsoft.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode)
   at Microsoft.Windows.Controls.DataGrid.CommitEdit(DataGridEditingUnit editingUnit, Boolean exitEditingMode)
   at Microsoft.Windows.Controls.DataGrid.CommitAnyEdit()
   at Microsoft.Windows.Controls.DataGrid.OnEnterKeyDown(KeyEventArgs e)
   at Microsoft.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.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, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
Exception calling "Run" with "1" argument(s): "Object reference not set to an instance of an object."
复制步骤:

  • 转到“新行”
  • 按F2键
  • 按回车键
这很好用:

  • 转到“新行”
  • 按F2键
  • 按ESC键
以及:

  • 转到“新行”
  • 按F2键
  • 输入“sdfdsf”
  • 按回车键
可以找到来源

我发现两个帖子似乎与这个问题有关:

  • 此外,我在C#中尝试了类似的代码,效果很好


    如何解决此问题?

    这似乎是Microsoft WPF DataGrid中的一个错误。如果你还没有这样做的话,你肯定应该通过报告

    作为一种解决方法,您可以在绑定上使用值转换器,以阻止将绑定目标上的空值传输到绑定源,这将导致NullReferenceException。以下是此类转换器的示例:

    // NullToUnsetConverter.cs    
    
    using System;
    using System.Windows;
    using System.Windows.Data;
    using System.Globalization;
    
    namespace MyProject.Controls.Converters
    {
        /// <summary>
        /// Converts <c>null</c> values to <see cref="DependencyProperty.UnsetValue"/>.
        /// <remarks>
        /// This converter is intented for use in situations when the binding target
        /// does not correctly handle <c>null</c> values. This is the case for example
        /// with some WPF UI controls.
        /// </remarks>
        /// </summary>
        public class NullToUnsetConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value == null)
                {
                    return DependencyProperty.UnsetValue;
                }
                else
                {
                    // No conversion applied
                    return value;
                }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                // No conversion applied
                return value;
            }
        }
    }
    
    //NullToUnsetConverter.cs
    使用制度;
    使用System.Windows;
    使用System.Windows.Data;
    利用制度全球化;
    命名空间MyProject.Controls.Converters
    {
    /// 
    ///将空值转换为空值。
    /// 
    ///此转换器用于绑定目标
    ///无法正确处理空值。例如,就是这种情况
    ///使用一些WPF UI控件。
    /// 
    /// 
    公共类NullToUnsetConverter:IValueConverter
    {
    公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
    {
    如果(值==null)
    {
    返回dependencProperty.unset值;
    }
    其他的
    {
    //未应用任何转换
    返回值;
    }
    }
    公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
    {
    //未应用任何转换
    返回值;
    }
    }
    }
    
    为了将转换器应用于绑定,您必须修改XAML,如下所示:

    <Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:cnv="clr-namespace:MyProject.Controls.Converters"
      xmlns:sys="clr-namespace:System;assembly=mscorlib"
      xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
      >
    
      <Window.Resources>
        <x:Array x:Key="people" Type="sys:Object" />
        <cnv:NullToUnsetConverter x:Key="NullToUnsetConverter" />
      </Window.Resources>
    
      <StackPanel>
        <dg:DataGrid ItemsSource="{DynamicResource people}" CanUserAddRows="True" AutoGenerateColumns="False">
          <dg:DataGrid.Columns>
    
            <dg:DataGridTextColumn Header="First" Binding="{Binding First, Converter={StaticResource NullToUnsetConverter}}" />
            <dg:DataGridTextColumn Header="Last" Binding="{Binding Last, Converter={StaticResource NullToUnsetConverter}}" />
    
          </dg:DataGrid.Columns>
        </dg:DataGrid>
      </StackPanel>
    </Window>
    
    
    
    或者,您可以修改NullToUnsetConverter.Convert方法,以在输入值为字符串时返回一个空字符串,就像您的例子一样