The selected ComboBox item binding does not indicate the initial value - then OK

advertisements

I have a weird problem with something simple I suppose. I have a combobox with two bindings set up - one for ItemsSource and another for SelectedItem.
The selected item is not working on initial startup, but then it works OK. Output does not indicate any binding problems, I have also set up a TextBlock with the same binding to see if it works - and it does.

Here's the code

  <ComboBox IsSynchronizedWithCurrentItem="True" IsEditable="False"
                          Name="ProgramsCollectionComboBox"
                          SelectedItem="{Binding ElementName=ThisUc,
                                                 Path=SelectedProgram}"
                          ItemsSource="{Binding ElementName=ThisUc,
                                                Path=ProgramsCollection}">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

                <TextBlock Text="{Binding ElementName=ThisUc,
                     Path=SelectedProgram.Name, Mode=TwoWay}" />

The property:

        private Program _selectedProgram;
        public Program SelectedProgram
        {
            get
            {
                if (_selectedProgram == null)
                {
                        _selectedProgram = new Program(Settings.Default.SelectedProgramPath);
                }
                return _selectedProgram;

            }
        set
        {
                _selectedProgram = value;
                Settings.Default.SelectedProgramPath = SelectedProgram.PathProgramFolder;
                RaisePropertyChanged("SelectedProgram");
        }
    }

It saves and reads the settings OK, the initial values is shown in the textblock below the combobox, when I change the selected item, the textblock is updated, the settings are changed and everything works fine - except for the fact that on app startup, selected item is not selected.

Thanks for help!


There are two reasons your initial binding is not working. First, as Jehof has mentioned himself, is the fact that you're setting your SelectedProgram to an item that is not part of the ProgramsCollection.

Furthermore, when you are setting the initial value of your SelectedProgram you are doing so in the getter, where PropertyChanged is not invoked and thus the binding will never be aware of that change. You can either invoke PropertyChanged when initializing it in the getter:

...
get
{
    if (_selectedProgram == null)
    {
        _selectedProgram = _programsCollection?.FirstOrDefault();
        RaisePropertyChanged("SelectedProgram");
    }
    return _selectedProgram;
}
...

Or even better, set the default value on the private field:

private Program _selectedProgram = _programsCollection?.FirstOrDefault();
...