Manvinder
Manvinder

Reputation: 4587

Setting SelectedItem of combo box in MVVM

I have a combo box, selectionchanged event of combo box attached with its respective command and upon changing the selected item(from the drop down list) command is firing as expected but not when I set the selected item from the code.

This is some code that I have tried

        Projects.Add(new Project { ProjectName = "Project 1" });
        Projects.Add(new Project { ProjectName = "Project 2" });
        Projects.Add(new Project { ProjectName = "Project 3" });
        Projects.Add(new Project { ProjectName = "Project 4" });  

        SelectedProject = Projects[0]; // I am expecting selection changed command execution here

   ICommand _projectSelectionChangedCommand;
   public ICommand ProjectSelectionChangedCommand
   {
     get
     {
       return _projectSelectionChangedCommand ?? (_projectSelectionChangedCommand = new RelayCommand(ProjectSelection));
     }
   }

 ObservableCollection<Project> _projects;
        public ObservableCollection<Project> Projects
        {
            get { return _projects; }
            set
            {
                _projects = value;


      NotifyPropertyChanged("Projects");
                }
            }

 public Project SelectedProject
        {
            get { return _selectedProject; }
            set
            {
                _selectedProject = value;
                NotifyPropertyChanged("SelectedProject");
            }
        }

and my xaml is this

   <ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Projects}" DisplayMemberPath="ProjectName" SelectedItem="{Binding SelectedProject}" >
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding ProjectSelectionChangedCommand}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ComboBox>

Upvotes: 0

Views: 2229

Answers (3)

Mark Feldman
Mark Feldman

Reputation: 16119

I have a vague recollection that SelectedProject has to be set to a valid instance before the binding is created, if it's null then the binding winds up permanently broken in the direction from data context to view.

Upvotes: 0

d.moncada
d.moncada

Reputation: 17392

Make sure that your ComboBox has TwoWay binding set for SelectedItem, as well as IsSynchronizedWithCurrentItem

   <ComboBox Grid.Row="1" Grid.Column="1" 
             ItemsSource="{Binding Projects}" 
             DisplayMemberPath="ProjectName"  
             SelectedItem="{Binding SelectedProject, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
             IsSynchronizedWithCurrentItem="True">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding ProjectSelectionChangedCommand}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ComboBox>

Upvotes: 1

David
David

Reputation: 10708

Make sure you use Mode=TwoWay in your SelectedItem Binding, else there is no guarantee that the selected item will change.

Also, be aware that trying to explicitly set a value not present in the Items available to the ComboBox will result in it instead setting a null. Attempting to manually set a null has additional oddities in behavior, indicating the underlying system uses a null value in between sets to actual values.

Upvotes: 1

Related Questions