0

I have the following problem:

When my view loads the combobox selected value is empty.

problem

Here is the code for my view:

<UserControl.Resources>
    ...
    <CollectionViewSource x:Key="SpecialtiesSource" Source="{Binding Path=Specialties}" />
    ...
</UserControl.Resources>

...

<ComboBox Grid.Row="11" Grid.Column="2" Name="Specialty" Style="{StaticResource ComboBoxItem}"
          SelectedIndex="0"
          IsEditable="True"
          TextSearch.TextPath="Name"
          DisplayMemberPath="Name" 
          ItemsSource="{Binding Source={StaticResource SpecialtiesSource}}" 
          SelectedItem="{Binding SelectedSpecialty}"
          SelectedValuePath="Name">
</ComboBox>

Here is my view model:

private List<Specialty> _specialties;
public List<Specialty> Specialties
{
    get
    {
        return _specialties;
    }

    set
    {
            _specialties = value;
            NotifyOfPropertyChange(() => Specialties);
        }
    }

    private Specialty _selectedSpecialty;
    public Specialty SelectedSpecialty
    {
        get
        {
            return _selectedSpecialty;
        }

        set
        {
            if (value == null)
            {
                return;
            }

            _selectedSpecialty = value;
            NotifyOfPropertyChange(() => SelectedSpecialty);
        }
    }

Model:

public class Specialty : IGeneric
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string TabColour { get; set; }
    public string TextColour { get; set; }
    public bool? ActiveFlag { get; set; }
}

public interface IGeneric
{
    string Name { get; set; }
}

When I am debugging the code I can see that my Specialties object is returned with the correct results, because when I click the drop down menu I can then see the all of the results as I would expect.

However immediately after it has loaded this object, I can see my SelectedSpecialty object then being hit which is null. I am guessing this is because of some underlying interaction between when the ItemsSource is loaded and then attempting to set the value for SelectedItem at index position 0?

Any input or advice on how I can figure out why SelectedItem is being set and how I can stop/set this correctly the the first item of my SpecialtiesSource?

I'm also not 100% confident I'm using the SelectedValuePath correctly.

user1839601
  • 119
  • 1
  • 9
  • Just want to make sure I understand. You can see the items in your CombBox after load, but the SelectedItem isn’t binding? So if you changed your selection your SelectedSpecialty is still null? – Tronald Apr 07 '18 at 13:21
  • Remove the selectedvaluepath line. How is SelectedSpeciality set initially? You're expecting it NOT to be null so something is shown as selected. BUT I see no code that sets it to anything. So it will be null. – Andy Apr 07 '18 at 14:14

1 Answers1

0

The bound SelectedItem starts at null. Same with ItemsSource. If you are seeing a list, you must initialize the Specialties somewhere that isn't shown in the question, just set the SelectedSpecialty after that.

Or if it is a static list loaded at run time, create the list in the get for Specialties. Then you can initialize SelectedSpecialty at runtime to one of the Specialties items.

rw_
  • 81
  • 11