简体   繁体   中英

ComboBox selected item binding not showing the initial value - then working OK

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM