将自定义对象绑定到 WPF 组合框

Binding Custom Object to WPF Combobox

我有一个组合框,其项目源类型为 ObservableCollection<Clinic>

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

此组合框位于从 EmployeeClinics 绑定的 ListView 中。

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

当我启动该应用程序时,我会看到相应的诊所。下拉列表似乎显示了正确的选项,但是当我更新它们时,只有名称更新而不是 ClinicId(它保留以前的 ClinicId)。

编辑: 同样,当我将新诊所添加到列表中并从选项中 select 时,当我查看集合时它的 ID 为 0。

这是我的诊所模型。

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

更新:谢谢@AyyappanSubramanian。我正在取得进展。我更新了我的对象

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>

更改下拉菜单现在可以正确更改基础对象并根据需要更新列表。现在我唯一的问题是组合框不显示当前对象,只是在开始时显示为空白。我没有运气就搞砸了 SelectedValue 和 Path。有什么建议吗?

参考以下代码。您可以使用 SelectedItem 获取一个 SelectedObject 中的 ID 和名称。使用 SelectedValue 仅获取 ID。

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

}