0

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?

8
  • Can you post you model and viewmodel to reproduce your issue. Commented Apr 19, 2015 at 19:29
  • What happens when you change the binding from StaticResource to DynamicResource? Commented Apr 19, 2015 at 19:35
  • @AyyappanSubramanian I have updated the post. Does this help? As in Fabio's answer, I've commented and can get the names to show up appropriately but the IDs dont update based on the new selections. Commented Apr 19, 2015 at 19:49
  • Add SelectedValuePath="ClinicId" in the ComboBox Commented Apr 19, 2015 at 19:53
  • @AyyappanSubramanian Ive posted new working code that shows it loading the correct items, I can't assign selectedvaluepath 2x. If i dont have Name in there it won't work as shown any longer. Commented Apr 19, 2015 at 20:12

1 Answer 1

3

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);
        }
    }

}
Sign up to request clarification or add additional context in comments.

4 Comments

I've successfully got the objects working (they link when changed) but the comboboxes don't show the current item correctly. Any suggestions? I've updated my question with the new info. They show correctly after I select one, but just show empty before that.
wait, didnt see this other part of the viewModel. let me update that too
Yeah, hmm cant seem to get them to initialize correctly.
YOu got my question answered, even though I came across a new issue in the mean time :( Thanks though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.