0
votes

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!

2
The getter of your property should return a value of your ProgramsCollection and not a new instance if it is nullJehof
@Jehof At initial start up, it should be null, therefore it should return the new Program anyway.Mike Eason
yes, but the new Programm is not part of your ProgrammsCollection. This is the cause why the initial value is not displayed, but afterwardsJehof

2 Answers

3
votes

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();
...
0
votes

The getter of your property SelectedProgram should return a value of your ProgrammsCollection and not a new instance if it is null.

If the value is not part of the collection that is bound to the combobox it is not displayed.