Search code examples
c#jsonwpfrestore

When restoring data from JSON into WPF the DisplayMemberPath is blank


I have a DataGridComboBox and both the ItemsSource and SelectedItemBinding are binding correctly.

XAML:

    <Grid>
        <Grid.Resources>
            <CollectionViewSource Source="{Binding BicnList}" x:Key="bicnList"/>
        </Grid.Resources>
        <DataGrid ItemsSource="{Binding DgNamingConventionRows}"
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="Category"
                                        ItemsSource="{Binding Source={StaticResource bicnList}}"
                                        DisplayMemberPath="CategoryName"
                                        SelectedItemBinding="{Binding BuiltInCategoryName, UpdateSourceTrigger=PropertyChanged}">
                     <DataGridComboBoxColumn.EditingElementStyle>
                         <Style TargetType="{x:Type ComboBox}"/>
                     </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
                <!--............!-->
           </DataGrid.Columns>
        </DataGrid>
     </Grid>

When opening the combobox all options are displayed correctly and when I select an item I can also see the DisplayMemberPath correctly.

enter image description here

I'm saving this data using json and this is also correctly done.

enter image description here

However, after closing the window, I want to restore this data when opening again the WPF. The data is restored and deserialization is working properly, but there is no display under the Category Combobox. The DisplayMemberPath is blank. What could be the cause of this issue? Any help would be appretiated.

enter image description here

C#:

    private ObservableCollection<BuiltInCategoryName> _bicnList = new
                ObservableCollection<BuiltInCategoryName>();
            
            [JsonIgnore]
            public ObservableCollection<BuiltInCategoryName> BicnList
            {
                get
                {
                    return _bicnList;
                }
                set
                {
                    _bicnList = value;
                    NotifyPropertyChanged("BicnList");
                }
            }
    public class BuiltInCategoryName : NotifierBase
    {
        //display Category.Name
        //save BuiltInCategory.Id
     
        public string CategoryName { get; set; }
     
        public BuiltInCategory BuiltInCategoryId { get; set; }
     
        public BuiltInCategoryName(Document doc, BuiltInCategory bic)
        {
            Category cat = null;
            try
            {
                cat = Category.GetCategory(doc, bic);
            }
            catch
            { }
            CategoryName = cat?.Name;
            BuiltInCategoryId = bic;
        }
        public BuiltInCategoryName()
        {
        }
    }
    public class ParameterRow : NotifierBase
        {
            private BuiltInCategoryName builtInCategoryName ;
            public BuiltInCategoryName BuiltInCategoryName 
            {
                get { return builtInCategoryName ; }
                set
                {
                    SetNotify(ref builtInCategoryName , value);
                }
            }
     
            private string patternText;
            public string PatternText
            {
                get { return patternText; }
                set
                {
                    SetNotify(ref patternText, value);
                }
            }
     
            private string comparisonValue;
            public string ComparisonValue
            {
                get { return comparisonValue; }
                set
                {
                    SetNotify(ref comparisonValue, value);
                }
            }
     
            public ParameterRow(BuiltInCategoryName category, string patternText, string comparison)
            {
                BuiltInCategoryName = category;
                PatternText = string.IsNullOrEmpty(patternText) ? "" : patternText;
                ComparisonValue = string.IsNullOrEmpty(patternText) ? "Prefix" : comparison;
            }
            public ParameterRow()
            {
            }
        }

Solution

  • What solved this issue was instead of using the SelectedItemBinding property I used SelectedValueBinding.

    XAML:

     <Grid>
        <Grid.Resources>
            <CollectionViewSource Source="{Binding BicnList}" x:Key="bicnList"/>
        </Grid.Resources>
        <DataGrid ItemsSource="{Binding DgNamingConventionRows}"
            <DataGrid.Columns>
                <DataGridComboBoxColumn Header="Category"
                                        ItemsSource="{Binding Source={StaticResource bicnList}}"
                                        DisplayMemberPath="CategoryName"
                                        SelectedValueBinding="{Binding BuiltInCategoryName, UpdateSourceTrigger=PropertyChanged}">
                     <DataGridComboBoxColumn.EditingElementStyle>
                         <Style TargetType="{x:Type ComboBox}"/>
                     </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
                <!--............!-->
           </DataGrid.Columns>
        </DataGrid>
     </Grid>