markokstate
markokstate

Reputation: 961

Binding Custom Object to WPF Combobox

I have a combobox that has an items source of type ObservableCollection<Clinic>

<ComboBox ItemsSource="{Binding Source={StaticResource ClinicList}}" DisplayMemberPath="Name" SelectedValue="{Binding Path=Name}" SelectedValuePath="Name"></ComboBox>

This combobox is within a ListView that is bound from EmployeeClinics.

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

When I launch the app I see the appropriate clinics. And the drop down seems to show the correct options, but when I update them, only the Name updates and not the ClinicId (it keeps previous ClinicId).

Edit: Similarly when I add a new clinic to the list and select it from the options, it's Id is 0 when I look at the collection.

enter image description here

Here is my clinic model.

public class Clinic {
        public int ClinicId { get; set; }
        public string _name { get; set; }
        public string Name {
            get {

            return _name;}
            set {
                if (_name != value) {
                    _name = value;
                }
            }
        }
}

UPDATE: Thanks @AyyappanSubramanian. I am making headway. I have updated my Objects

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


public class ClinicView {

    private Clinic selectedClinic;
    public Clinic SelectedClinic {
        get { return selectedClinic; }
        set {
            selectedClinic = value;
            selectedClinicId = selectedClinic.ClinicId;
        }
    }

    private int selectedClinicId;
    public int SelectedClinicId {
        get { return selectedClinicId; }
    }
}

XAML:

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

Changing the drop downs now properly changes the underlying object and updates the list as desired. Now my only issue is that the comboboxes don't display the current object, just show as blank on start. I've messed around with SelectedValue and Path with no luck. Any suggestions?

Upvotes: 0

Views: 5567

Answers (1)

Ayyappan Subramanian
Ayyappan Subramanian

Reputation: 5366

Refer the below code. You can use SelectedItem to get both the ID and Name in one SelectedObject. Get only ID using SelectedValue.

 <ComboBox ItemsSource="{Binding Clinics}" DisplayMemberPath="ClinicName"
              SelectedValuePath="ClinicId" SelectedValue="{Binding SelectedClinicId}"                 
              SelectedItem="{Binding SelectedClinic}"/>


 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }        
}

class Clinic
{
    public int ClinicId { get; set; }
    public string ClinicName { get; set; }
}

class ViewModel
{
    public ObservableCollection<Clinic> Clinics { get; set; }
    public ViewModel()
    {
        Clinics = new ObservableCollection<Clinic>();
        for (int i = 0; i < 10; i++)
        {
            Clinics.Add(new Clinic() { ClinicId=i+1,ClinicName="MyClinic"+(i+1) });
        }
    }

    private int selectedClinicId;

    public int SelectedClinicId
    {
        get { return selectedClinicId; }
        set 
        { 
            selectedClinicId = value;
        }
    }


    private Clinic selectedClinic;

    public Clinic SelectedClinic
    {
        get { return selectedClinic; }
        set 
        { 
            selectedClinic = value;
            MessageBox.Show("ID:"+selectedClinic.ClinicId.ToString()+" "+"Name:"+selectedClinic.ClinicName);
        }
    }

}

Upvotes: 3

Related Questions