C# WPF将ObservableCollection绑定到ComboBox

C# WPF将ObservableCollection绑定到ComboBox,c#,wpf,xaml,binding,combobox,C#,Wpf,Xaml,Binding,Combobox,我尝试绑定镜头对象列表,并希望在组合框中显示LensName属性。我代码中的列表包含对象,但组合框仍然为空或属性不显示。我已经尝试了所有已知的绑定数据的方法,但没有结果。感谢您的帮助 Xaml 代码隐藏 public observetecollection RightBestlensList=new observetecollection(); public ObservableCollection LeftBestlensList=新的ObservableCollection(); if(

我尝试绑定镜头对象列表,并希望在组合框中显示LensName属性。我代码中的列表包含对象,但组合框仍然为空或属性不显示。我已经尝试了所有已知的绑定数据的方法,但没有结果。感谢您的帮助

Xaml


代码隐藏

public observetecollection RightBestlensList=new observetecollection();
public ObservableCollection LeftBestlensList=新的ObservableCollection();
if(OphtalboxIA.OphtalboxComputingModule.LeftBestLensList!=null)
{
如果(OphtalboxIA.OphtalboxComputingModule.LeftBestLensList.Count>0)
{        
LeftBestlensList=新的ObservableCollection(OphtalboxIA.OphtalboxComputingModule.LeftBestlensList);
//LeftbestlensCombo.ItemsSource=LeftBestlensList;
}
}
if(OphtalboxIA.OphtalboxComputingModule.RightBestLensList!=null)
{
如果(OphtalboxIA.OphtalboxComputingModule.RightBestLensList.Count>0)
{
RightBestlensList=新的ObservableCollection(OphtalboxIA.OphtalboxComputingModule.RightBestlensList);
//RightbestlensCombo.ItemsSource=RightBestlensList;
}
}
我的班级镜头

[xmlclude(镜头类型))]
公共级镜头{
公共字符串LensName;
公共弦透镜;
公共弦透镜;
公共浮子直径;
公众浮标半径;
公共浮球;
公共浮筒;
公共int轴;
公共字符串加法;
公共字符串描述;
公共int isRX;
公共int等苯盒;
公共优先秩序;
公共int LensFrequencyId;
公共字符串LensFrequencyName;
公共透镜体;
公共内部透镜材料;
}

您需要的是属性,而不是字段。这些字段是:

public ObservableCollection<OphtalboxIA.Lens> RightBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
public ObservableCollection<OphtalboxIA.Lens> LeftBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
public observetecollection RightBestlensList=new observetecollection();
public ObservableCollection LeftBestlensList=新的ObservableCollection();
作为属性,它们将如下所示:

private readonly ObservableCollection<OphtalboxIA.Lens> _rightList = new ObservableCollection<OphtalboxIA.Lens>();
private readonly ObservableCollection<OphtalboxIA.Lens> _leftList = new ObservableCollection<OphtalboxIA.Lens>();

public ObservableCollection<OphtalboxIA.Lens> RightBestlensList { get { return _rightList; }}
public ObservableCollection<OphtalboxIA.Lens> LeftBestlensList { get { return _leftList; }}
private readonly observedcollection _rightList=new observedcollection();
私有只读ObservableCollection _leftList=新ObservableCollection();
公共ObservableCollection RightBestlensList{get{return}rightList;}
公共ObservableCollection LeftBestlensList{get{return}

此外,绑定中还有一个输入错误:
Source=LefttBestLensList
。(一个额外的“t”)和外壳是错误的(“…镜头…”与“…镜头…”)。

RightBestlensList
LeftBestlensList
必须在ViewModel类中,而不是在代码隐藏中,并且它们必须是属性。

您必须尝试下面的代码

您必须在ViewModel中声明ObservableCollection,如

 private ObservableCollection<Lens> _RightBestLensList = new ObservableCollection<Lens>();

    public ObservableCollection<Lens> RightBestLensList
    {
        get { return _RightBestLensList; }
        set { _RightBestLensList = value; RaisePropertyChanged("RightBestLensList"); }
    }

绑定后是否填充属性?如果是这样,那么原因是您的
Lens
类没有实现
INotifyPropertyChanged
。这是将值推送到视图所必需的,在这种情况下,ComboBox需要实现该接口,以便收到值的任何更改的通知,从而可以将它们拉到视图中。不,属性已经填充…然后我进行绑定…为什么要花时间回答一个问题,但投入的精力却很少?您没有看到Stack overflow提供的通常高质量的答案吗?为什么你会用你的一句话来破坏这一点,而这句话也可能是一个评论?
[XmlInclude(typeof(Lens))]
public class Lens
{
    public string LensName { get; set; }
    public string  LensType { get; set; }

}