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

时间:2011-04-03 08:23:32

标签: c# .net wpf wpfdatagrid

我有一个简单的应用程序,它从DataBase中读取Albums列表并填充ListBox(AlbumShowCase)。 每当选择一个ListBoxItem时,我用该Album中的轨道列表(也来自DataBase)更新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...
    }

3 个答案:

答案 0 :(得分:1)

我怀疑这是因为你绑定了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将尝试“新”一个来设置属性。

答案 1 :(得分:1)

使用.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跟踪一个,在SO上查看关于此主题的这个主题:How to detect broken WPF Data binding?

答案 2 :(得分:0)

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;

    }