简体   繁体   中英

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>

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.

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>

我有一个模糊的回忆,在创建绑定之前必须将SelectedProject设置为有效的实例,如果它为null,则绑定会在从数据上下文到视图的方向上永久断开。

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