我尝试绑定镜头对象列表,我想在我的组合框中显示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;
}
}
我的班级镜头
[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;
}
答案 0 :(得分:1)
您需要属性,而不是字段。这些是字段:
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
。 (一个额外的&#34; t&#34;)外壳是错误的(&#34; ......镜头......&#34; vs.&#34; ......镜头......&#34; )。
答案 1 :(得分:0)
RightBestlensList
和LeftBestlensList
必须位于ViewModel类中,而不是Code Behind中,它们必须是属性。
答案 2 :(得分:0)
你必须尝试下面的代号。
你必须在ViewModel Like
中声明ObservableCollection private ObservableCollection<Lens> _RightBestLensList = new ObservableCollection<Lens>();
public ObservableCollection<Lens> RightBestLensList
{
get { return _RightBestLensList; }
set { _RightBestLensList = value; RaisePropertyChanged("RightBestLensList"); }
}
你的镜片课程应该是
[XmlInclude(typeof(Lens))]
public class Lens
{
public string LensName { get; set; }
public string LensType { get; set; }
}