0

I have a combobox and I am binding the combobox in XAML with viewmodel dictionary values.

When the page is loaded for first time I am trying to download the dictionary values and from server and set it to the dictionary view model variable.

But the combobox looks empty I don't understand why this happens because View Model variable has been updated and thats supposed to trigger the reload of combobox and thats not happening..

FYI: If I hardcode the dictionary rather than downloading it from server I don't see this problem When I load the page second time I don't see this problem

Update

XAML

<ComboBox x:Name=“testBox” Margin=“0,0,0,0” PlaceholderText="{StaticResource testText}” ItemsSource="{Binding TestDictionary.Values}” SelectedValue="{Binding DictionaryValue, Mode=TwoWay}" IsEnabled="{Binding IsItLoading, Converter={StaticResource InverseBooleanConverter}}"/>

View Model

       private Dictionary<string, string> testDictionary;


 public Dictionary<string, string> TestDictionary
        {
            get
            {
                if (this.testDictionary == null)
                {
                    this.testDictionary = new Dictionary<string, string>();
                }

                return this.testDictionary;
            }

            set
            {
                this.Set(() => this.TestDictionary, ref this.testDictionary, value);
            }
        }
Ranjith kumar
  • 239
  • 1
  • 4
  • 11

1 Answers1

1

The Dictionary does not provides notifications when items get added, removed, or when the entire list is refreshed.

When we add new data to the Dictionary, we should be able to set null to the ItemsSource of the ComboBox. Then set the TestDictionary.Values to ItemsSource of the ComboBox.

You can also implemented your own ObservableDictionary. When the Dictionary changes, the ComboBox will be changed. To implement the ObservableDictionary, you can refer the following question.

Jayden
  • 3,276
  • 1
  • 10
  • 14