【发布时间】:2011-07-28 13:25:54
【问题描述】:
我有一个简单的应用程序,它从数据库中读取专辑列表并填充列表框 (AlbumShowCase)。 Whenever a ListBoxItem is selected I update a DataGrid (trackDataGrid) with the list of tracks in that Album (also from the DataBase).
问题是,我可以编辑 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...
}
【问题讨论】:
-
我想查看 XAML 声明
标签: c# .net wpf wpfdatagrid