Hello. I'm getting a weird error and I have no idea why. Here is my setup:
I put the XamMultiColumnComboEditor into a field in my xamdatagrid.
<Style x:Key="VGStatusCodesStyle" TargetType="{x:Type igWPF:XamTextEditor }"> <Setter Property="EditTemplate"> <Setter.Value> <ControlTemplate> <ig:XamMultiColumnComboEditor Name="VGStatusCodes" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.ValveGroupStatusCodes}" DisplayMemberPath="StatusCode" SelectedValuePath="StatusCode" Width="80" SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Mode=TwoWay, Converter={StaticResource StatusCodesToStringConverter}}" MaxDropDownWidth="275" MaxDropDownHeight="270"> </ig:XamMultiColumnComboEditor> </ControlTemplate> </Setter.Value> </Setter>
public class StatusCodesToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return Binding.DoNothing; }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value is ValveGroupStatusCode) return (value as ValveGroupStatusCode).StatusCode;
return Binding.DoNothing; } }
in the view model I'm handling the EditModeEnded and CellChanged event for the xamdatagrid. So if someone pics a particular row from the XamMultiColumnComboEditor I have to delete that row automatically Sequence of events:
1.The CellChanged event executes and in it I call the end edit mode method ->e.Cell.EndEditMode();
2. The code goes into the EditModeEnded method and here is where I want to delete the row whos cell changed:
var ID = e.Cell.Record.Cells[0].Value;
LogData.Rows.Find(ID).Delete(); //LogData is bound to my xamdatagrid, it's a DataTable
If I do all this on a small test project I set up, everything works fine. But it does not in my main project. I can't seem to reproduce the problem.
I traced the issue to this in the style
SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Mode=TwoWay, Converter={StaticResource StatusCodesToStringConverter}}"
So if I take this code away the row get deleted without issue else I get this error:
Internal error: internal WPF code tried to reactivate a BindingExpression that was already marked as detached.
System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=Value; DataItem=null; NullReferenceException:'System.NullReferenceException: Object reference not set to an instance of an object
Why would or what is causing the DataItem be null? --> Path=Value; DataItem=null;
I wondering you someone might give me a hint or an idea where to look.
Thanks
Hi Kris,
Does your full error look like this:
System.Windows.Data Error: 8 : Cannot save value from target back to source. BindingExpression:Path=Value; DataItem=null; NullReferenceException:'System.NullReferenceException: Object reference not set to an instance of an object. at Infragistics.Windows.DataPresenter.CellValuePresenter.OnValueChanged() at Infragistics.Windows.Editors.ValueEditor.OnValueChanged(Object previousValue, Object currentValue) at Infragistics.Windows.Editors.ValueEditor.OnValueChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at Infragistics.Windows.Editors.ValueEditor.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at Infragistics.Windows.Editors.TextEditorBase.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal) at MS.Internal.Data.PropertyPathWorker.SetValue(Object item, Object value) at MS.Internal.Data.ClrBindingWorker.UpdateValue(Object value) at System.Windows.Data.BindingExpression.UpdateSource(Object value)'
If so then I have reproduced your issue. From what I can see, the issue is that removing the row is occurring in the middle of the edit event which is then triggering some value change stuff. Before removing the row you need to let the edit state finish completely. You can wait for it to finish by using a Dispatcher.BeginInvoke();
Dispatcher.BeginInvoke(new Action(() =>{ var ID = e.Cell.Record.Cells[0].Value; LogData.Rows.Find(ID).Delete();}));
This code inside the EditModeEnded event will let the event finish before it executes the row deletion. This will avoid any weird states where the row gets deleted but the grid is still trying to work with it during this event.
Yes, that is the exact error I'm getting. The dispatcher stuff solved my issue. Strangely this does not happen in my own test project where I tried to reproduce the error. Thanks.
Yeah that's pretty weird. I was able to get this error to show up almost immediately. Glad it solved the issue in your main application though.