Dear customer support team,
I am trying to create a custom editor for the SfPropertyGrid using a ComboBoxAdv control. The items are loaded from a database. When I try the set the ComboBoxAdv.ItemsSource I get a null reference exception. If I use the exact same code with a regular ComboBox, the ItemsSource is populated. Any ideas how to solve this would be helpful.
Thanks in advance,
NIels
Below is the error I get:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Syncfusion.Shared.WPF
StackTrace:
at Syncfusion.Windows.Tools.Controls.ComboBoxAdv.FilterItem(Object value)
at System.Windows.Data.ListCollectionView.PrepareLocalArray()
at System.Windows.Data.ListCollectionView.RefreshOverride()
at System.Windows.Data.CollectionView.RefreshInternal()
at System.Windows.Data.CollectionView.RefreshOrDefer()
at System.Windows.Data.CollectionView.set_Filter(Predicate`1 value)
at System.Windows.Data.ListCollectionView.set_Filter(Predicate`1 value)
at Syncfusion.Windows.Tools.Controls.ComboBoxAdv.OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.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 System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
at EPD3.Infrastructure.SFControls.SfPropertyGrid.CustomEditors.HealthProfessionalEditor.<combo_Loaded>d__14.MoveNext() in C:\Users\Niels\source\repos\SyncFusionDemoApp\EPD3\Infrastructure\SFControls\SfPropertyGrid\CustomEditors\HealthProfessionalEditor.cs:line 86
Below here is the code I have in my Editor:
using EPDMediatRLibrary.Models.API.AppData.V1.Contact.Person.Practitioner;
using EPDMediatRLibrary.Queries.API.AppData.V1.Contact.Person.Practitioner;
using Syncfusion.Windows.PropertyGrid;
using Syncfusion.Windows.Tools.Controls;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace EPD3.Infrastructure.SFControls.SfPropertyGrid.CustomEditors
{
internal class HealthProfessionalEditor : ITypeEditor
{
public IEnumerable<PractitionerHealthcareProviderCompanyDisplayModel> GPItemsSourceOrig { get; set; }
private ObservableCollection<PractitionerHealthcareProviderCompanyDisplayModel> CBBItemsSource { get; set; }
private ComboBoxAdv combo;
private readonly IMediator _mediator;
public HealthProfessionalEditor(IMediator mediator)
{
_mediator = mediator;
CBBItemsSource = new();
}
public void Attach(PropertyViewItem property, PropertyItem info)
{
if (info.CanWrite)
{
var binding = new Binding("Value")
{
Mode = BindingMode.TwoWay,
Source = info,
ValidatesOnExceptions = true,
ValidatesOnDataErrors = true
};
BindingOperations.SetBinding(combo, System.Windows.Controls.Primitives.Selector.SelectedItemProperty, binding);
combo.IsTabStop = true;
combo.IsHitTestVisible = true;
combo.IsEnabled = true;
}
else
{
var binding = new Binding("Value")
{
Source = info,
ValidatesOnExceptions = true,
ValidatesOnDataErrors = true
};
BindingOperations.SetBinding(combo, System.Windows.Controls.Primitives.Selector.SelectedItemProperty, binding);
combo.IsTabStop = false;
combo.IsHitTestVisible = false;
combo.IsEnabled = false;
}
}
public object Create(PropertyInfo PropertyInfo)
{
combo = new()
{
IsEditable = true,
AutoCompleteMode = AutoCompleteModes.Suggest,
DisplayMemberPath = "PractitionerDisplayName",
};
combo.Loaded += new RoutedEventHandler(combo_Loaded);
return combo;
}
public void Detach(PropertyViewItem property)
{
//throw new NotImplementedException();
}
private async void combo_Loaded(object sender, RoutedEventArgs e)
{
CancellationToken cancellationToken = new();
GPItemsSourceOrig = await _mediator.Send(new GetGeneralPractitionersHealthcareProviderCompanyDisplayModelsQuery(), cancellationToken);
if (GPItemsSourceOrig != null && GPItemsSourceOrig.Any())
{
CBBItemsSource = new ObservableCollection<PractitionerHealthcareProviderCompanyDisplayModel>(Enumerable.DistinctBy(GPItemsSourceOrig, x => x.HealthcareProviderCompanyId));
CBBItemsSource.Insert(0, new PractitionerHealthcareProviderCompanyDisplayModel() { HealthcareCompanyDisplayName = "" });
combo.ItemsSource = CBBItemsSource; // here is where the error occurs
}
}
}
}
Thank you! Probably there is something wrong with my code then. If I run into this problem again, I will report it here.
Kind regards,
Niels van Strien