我有一個簡單的應用程序,它從DataBase中讀取專輯列表並填充ListBox(AlbumShowCase)。 每當一個ListBoxItem被選中,我更新一個DataGrid(trackDataGrid)與該專輯中的軌道列表(也來自DataBase)。在WPF中編輯DataGrid會導致System.NullReferenceException異常
問題是,我可以編輯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聲明 – ebattulga 2011-04-03 10:06:00