WPF将ObservableCollection绑定到ComboBox
本文关键字:ComboBox 绑定 ObservableCollection WPF | 更新日期: 2023-09-27 18:06:57
我尝试绑定一个Lens Objects列表,我想在我的组合框中显示LensName属性。代码中的列表包含对象,但组合框仍然为空或不显示属性。我已经尝试了所有已知的方法来绑定我的数据,但没有结果。谢谢你的帮助
Xaml
<ComboBox x:Name="RightbestlensCombo" ItemsSource="{Binding Source=RightBestLensList}" DisplayMemberPath="LensName" SelectedValuePath="LensTypeId" />
<ComboBox x:Name="LeftbestlensCombo" ItemsSource="{Binding Source=LefttBestLensList}" DisplayMemberPath="LensName" SelectedValuePath="LensTypeId" ></ComboBox>
背后的代码 public ObservableCollection<OphtalboxIA.Lens> RightBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
public ObservableCollection<OphtalboxIA.Lens> LeftBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
if (OphtalboxIA.OphtalboxComputingModule.LeftBestLensList != null)
{
if (OphtalboxIA.OphtalboxComputingModule.LeftBestLensList.Count > 0)
{
LeftBestlensList=new ObservableCollection<OphtalboxIA.Lens>(OphtalboxIA.OphtalboxComputingModule.LeftBestLensList);
//LeftbestlensCombo.ItemsSource = LeftBestlensList;
}
}
if (OphtalboxIA.OphtalboxComputingModule.RightBestLensList != null)
{
if (OphtalboxIA.OphtalboxComputingModule.RightBestLensList.Count > 0)
{
RightBestlensList=new ObservableCollection<OphtalboxIA.Lens>(OphtalboxIA.OphtalboxComputingModule.RightBestLensList);
//RightbestlensCombo.ItemsSource = RightBestlensList;
}
}
My class Lens
[XmlInclude(typeof(Lens))]
public class Lens{
public String LensName;
public String LensType;
public String LensTypeTrial;
public float Diameter;
public float Radius;
public float Sphere;
public float Cylinder;
public int Axis;
public String Addition;
public String Description;
public int isRX;
public int isOphtalBox;
public int priorityOrder;
public int LensFrequencyId;
public string LensFrequencyName;
public int LensTypeId;
public int LensMaterialId;
}
您需要的是属性,而不是字段。这些是字段:
public ObservableCollection<OphtalboxIA.Lens> RightBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
public ObservableCollection<OphtalboxIA.Lens> LeftBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
作为属性,它们看起来像这样:
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; }}
此外,绑定中有一个错别字:Source=LefttBestLensList
。(多了一个"t")并且外壳是错误的("…Lens…"vs。"……镜头……")。
RightBestlensList
和LeftBestlensList
必须在ViewModel类中,而不是在Code Behind中,并且它们必须是属性
您必须尝试下面提到的代码。
你必须在ViewModel中声明ObservableCollection
private ObservableCollection<Lens> _RightBestLensList = new ObservableCollection<Lens>();
public ObservableCollection<Lens> RightBestLensList
{
get { return _RightBestLensList; }
set { _RightBestLensList = value; RaisePropertyChanged("RightBestLensList"); }
}
你的Lens类应该是
[XmlInclude(typeof(Lens))]
public class Lens
{
public string LensName { get; set; }
public string LensType { get; set; }
}