在 WPF 中编辑 DataGrid 会导致 System.NullReferenceException

发布于 2024-10-29 20:29:03 字数 4666 浏览 3 评论 0原文

我有一个简单的应用程序,它从数据库读取专辑列表并填充列表框(AlbumShowCase)。 每当选择 ListBoxItem 时,我都会使用该专辑中的曲目列表(也来自数据库)更新 DataGrid (trackDataGrid)。

问题是,我可以编辑 DataGrid 中的项目,并且对于所有现有轨道,更改是持久的。但是,如果我尝试添加新曲目,一旦完成编辑该行,我就会收到 System.NullReferenceException。

private TunesDBDataContext db;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    db = new TunesDBDataContext("TunesDB.sdf");
    var query = from a in db.Albums select new AlbumCase(a);
    AlbumShowCase.ItemsSource = query;
}

private void trackDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    db.SubmitChanges();
}

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var query = from a in db.Albums
                where a.AlbumID == ((AlbumCase)e.AddedItems[0]).Album.AlbumID
                select a.Tracks;

    trackDataGrid.ItemsSource = query;
}

异常发生在我的 ValueConverter 之后:

[ValueConversion(typeof(String), typeof(int))]
public class TimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int time = (int)value;
        TimeSpan ts = TimeSpan.FromSeconds(time);
        return ts.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // The validation runs before this, so we know that if we got here
        // the data must be valid and won't throw an exception.
        return (int)TimeSpan.Parse((string)value).TotalSeconds;
        // THE EXCEPTION OCCURS AFTER THIS LINE FOR NEW ROWS
    }

TimeConverter 与 TimeConverterRule 配对,确保输入的轨道长度有效,而且据我所知,它工作正常。 仅当用户编辑 DataGrid 的最后一行(空行)时才会发生崩溃。这是堆栈跟踪:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=PresentationFramework
StackTrace:
    at System.Windows.Data.BindingExpression.IsValidValueForUpdate(Object value, Type sourceType)
    at System.Windows.Data.BindingExpression.ConvertProposedValue(Object value)
    at System.Windows.Data.BindingExpression.ValidateAndConvertProposedValue(Collection1& values)
    at System.Windows.Controls.DataGridHelper.ValidateWithoutUpdate(FrameworkElement element)
    at System.Windows.Controls.DataGridColumn.CommitCellEdit(FrameworkElement editingElement)
    at System.Windows.Controls.DataGridColumn.CommitEdit(FrameworkElement editingElement)
    at System.Windows.Controls.DataGridCell.CommitEdit()
    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(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 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)
    etc...etc...
    }

I have a simple application that reads a list of Albums from a DataBase and fills a ListBox (AlbumShowCase).
Whenever a ListBoxItem is selected I update a DataGrid (trackDataGrid) with the list of tracks in that Album (also from the DataBase).

The problem is, I can edit the items in the DataGrid, and for all the existing tracks, changes are persistent. But if I try to add a new track, once I finish editing the row I get the System.NullReferenceException.

private TunesDBDataContext db;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    db = new TunesDBDataContext("TunesDB.sdf");
    var query = from a in db.Albums select new AlbumCase(a);
    AlbumShowCase.ItemsSource = query;
}

private void trackDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    db.SubmitChanges();
}

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var query = from a in db.Albums
                where a.AlbumID == ((AlbumCase)e.AddedItems[0]).Album.AlbumID
                select a.Tracks;

    trackDataGrid.ItemsSource = query;
}

The Exception occurs right after my ValueConverter:

[ValueConversion(typeof(String), typeof(int))]
public class TimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int time = (int)value;
        TimeSpan ts = TimeSpan.FromSeconds(time);
        return ts.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // The validation runs before this, so we know that if we got here
        // the data must be valid and won't throw an exception.
        return (int)TimeSpan.Parse((string)value).TotalSeconds;
        // THE EXCEPTION OCCURS AFTER THIS LINE FOR NEW ROWS
    }

The TimeConverter is paired with a TimeConverterRule that makes sure the track length entered is valid, and for all I know it's working fine.
It's just when the user edits the LAST row (the empty one) of the DataGrid that the crash occurs. And here's the stack trace:

System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=PresentationFramework
StackTrace:
    at System.Windows.Data.BindingExpression.IsValidValueForUpdate(Object value, Type sourceType)
    at System.Windows.Data.BindingExpression.ConvertProposedValue(Object value)
    at System.Windows.Data.BindingExpression.ValidateAndConvertProposedValue(Collection1& values)
    at System.Windows.Controls.DataGridHelper.ValidateWithoutUpdate(FrameworkElement element)
    at System.Windows.Controls.DataGridColumn.CommitCellEdit(FrameworkElement editingElement)
    at System.Windows.Controls.DataGridColumn.CommitEdit(FrameworkElement editingElement)
    at System.Windows.Controls.DataGridCell.CommitEdit()
    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(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 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)
    etc...etc...
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

会发光的星星闪亮亮i 2024-11-05 20:29:03

我怀疑这是因为您绑定到 LINQ to SQL 查询的结果。当您编辑行时,WPF 会尝试将新项目“添加”到您的查询中 - 但它不支持添加。

尝试这样的操作:

var query = from a in db.Albums
            where a.AlbumID == ((AlbumCase)e.AddedItems[0]).Album.AlbumID
            select a.Tracks;

var dataSource = new ObservableCollection<Track>();
foreach (var item in query)
    dataSource.Add(item);

trackDataGrid.ItemsSource = dataSource;

然后您可能需要订阅网格上的事件,以便在添加项目时将其添加到 DbContext。

另外,请确保AlbumCase 是具有公共无参数构造函数的公共类。这是因为 WPF 将尝试“新建”一项来设置属性。

My suspicion is that this is because you are binding to the results of a LINQ to SQL query. When you edit the row, WPF tries to "Add" the new item to your query - but it doesn't support adding.

Try something like this:

var query = from a in db.Albums
            where a.AlbumID == ((AlbumCase)e.AddedItems[0]).Album.AlbumID
            select a.Tracks;

var dataSource = new ObservableCollection<Track>();
foreach (var item in query)
    dataSource.Add(item);

trackDataGrid.ItemsSource = dataSource;

You might then need to subscribe to events on the grid so that when an item is added, you add it to the DbContext.

Also, make sure AlbumCase is a public class with a public, parameterless constructor. That's because WPF will try to "new" one up to set properties on.

忘年祭陌 2024-11-05 20:29:03

使用.NET Reflector,这是您遇到的代码(在 System.Windows.Data.BindingExpression 中):

internal override object ConvertProposedValue(object value)
{
    ...
    Type sourcePropertyType = this.Worker.SourcePropertyType;
    IValueConverter dynamicConverter = null;
    CultureInfo culture = base.GetCulture();
    if (this.Converter != null)
    {
        if (!base.UseDefaultValueConverter)
        {
            value = this.Converter.ConvertBack(value, sourcePropertyType, this.ParentBinding.ConverterParameter, culture);
            if (((value != Binding.DoNothing) && (value != DependencyProperty.UnsetValue)) && !this.IsValidValueForUpdate(value, sourcePropertyType))
            {
                dynamicConverter = this.DynamicConverter;
            }
        }

据我所知,“SourcePropertyType”是一个空值(然后它抛出对 IsValidValueForUpdate)。

因此,这显然是PresentationFramework 中的一个错误(它应该报告一个不错的错误并优雅地失败),但它之所以发生是因为不知何故,您向WPF 传递了一个为null 的源属性类型。也许是因为通用开放类型或匿名类型。

希望这有帮助。

为了帮助您进行诊断,我建议您将 WPF 跟踪转为一,请参阅有关此主题的此主题: 如何检测损坏的 WPF 数据绑定?

Poking with .NET Reflector, this is the code you're running into (in System.Windows.Data.BindingExpression):

internal override object ConvertProposedValue(object value)
{
    ...
    Type sourcePropertyType = this.Worker.SourcePropertyType;
    IValueConverter dynamicConverter = null;
    CultureInfo culture = base.GetCulture();
    if (this.Converter != null)
    {
        if (!base.UseDefaultValueConverter)
        {
            value = this.Converter.ConvertBack(value, sourcePropertyType, this.ParentBinding.ConverterParameter, culture);
            if (((value != Binding.DoNothing) && (value != DependencyProperty.UnsetValue)) && !this.IsValidValueForUpdate(value, sourcePropertyType))
            {
                dynamicConverter = this.DynamicConverter;
            }
        }

From what I can understand the "SourcePropertyType" is a null value (and then it throws in the call to IsValidValueForUpdate).

So, this is clearly a bug in PresentationFramework (it sould report a nice error and fail gracefully), but it happens because somehow, you pass to WPF a source property type that is null. Maybe because of a generic open type or an anonymouse type.

Hope this helps.

To help you diagnose, I suggest you turn WPF traces one, see this thread on this subject on SO: How to detect broken WPF Data binding?

夏日落 2024-11-05 20:29:03
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
//check value null
if(value==null) return 0;
        return (int)TimeSpan.Parse((string)value).TotalSeconds;

    }
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
//check value null
if(value==null) return 0;
        return (int)TimeSpan.Parse((string)value).TotalSeconds;

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文