将自定义对象绑定到WPF Combobox

时间:2015-04-19 19:25:19

标签: wpf data-binding 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; }
}

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

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

enter image description here

这是我的诊所模型。

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而没有运气。有什么建议吗?

1 个答案:

答案 0 :(得分:3)

请参阅以下代码。您可以使用SelectedItem在一个SelectedObject中同时获取ID和Name。使用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);
        }
    }

}