Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox doesnt initialize with SelectedValue WPF

Having issues getting my Combobox to Initialize with the current selecteditem.

ClinicList returns ObservableCollection<Clinic>

      <ComboBox ItemsSource="{Binding Source={StaticResource ClinicList}}"
 DisplayMemberPath="Name" SelectedItem="{Binding SelectedClinic, 
    UpdateSourceTrigger=PropertyChanged}"></ComboBox>

My ComboBox is within a listview bound to an employee. An employee can have multiple Clinics assigned.

public class Employee{ 
...
public ObservableCollection<ClinicView> EmployeeClinics { get; set; }
}

public class ClinicView:INotifyPropertyChanged {

        private Clinic selectedClinic;
        public Clinic SelectedClinic {
            get { return selectedClinic; }
            set {
                selectedClinic = value;
                OnPropertyChanged("SelectedClinic");
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName) {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
public class Clinic{
   public int ClinicId{get;set;}
   public string Name {get;set;
}

The combobox shows as blank when first loaded, and will have the correct # of clinics per Employee as assigned. The combobox drop down has the correct Clinics in it, and when I select one, it replaces the blank combobox text and updates the employee correctly.

My question is why doesnt the Combobox Initially fill with correct text and is blank?

enter image description here

Edit: ClinicList is being set via the ODP in window.resources

 public static ObservableCollection<Clinic> GetClinicList() {
//gets list of clinics from db with ID
}

 <ObjectDataProvider MethodName="GetClinicList"
                            ObjectType="{x:Type local:MappingGenerators}"
                            x:Key="ClinicList">
        </ObjectDataProvider>
like image 918
markokstate Avatar asked Dec 20 '25 23:12

markokstate


1 Answers

Found this post to be a life saver.

https://rachel53461.wordpress.com/2011/08/20/comboboxs-selecteditem-not-displaying/

Here are the key passages:

The problem is simple: By default WPF compares SelectedItem to each item in the ItemsSource by reference, meaning that unless the SelectedItem points to the same item in memory as the ItemsSource item, it will decide that the item doesn’t exist in the ItemsSource and so no item gets selected.

To work around this, you can either use the ComboBox’s SelectedValue and SelectedValuePath to set the SelectedItem by Value instead of by Item

like image 117
markokstate Avatar answered Dec 23 '25 18:12

markokstate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!