2014-06-05 18 views
0

我尝试绑定镜头的对象列表,我想在我的代码我combobox.My列表中显示的LensName属性包含对象,但组合框保留为空或属性不display.I已经尝试过已知结合我的数据,而不result.Thanks的所有方式帮助WPF绑定的ObservableCollection到组合框

的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

的性能得到绑定后填充?如果是这样的话,原因是你的Lens类没有实现INotifyPropertyChanged。这需要将值查看,在这种情况下,需要组合框,为了实现该接口得到通知价值的任何变化,以便它可以拉他们查看。 – pushpraj

+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。 (一个额外的“t”)和外壳是错误的(“......镜头......”与“......镜头......”)。

0

RightBestlensListLeftBestlensList必须在视图模型类,而不是在后面的代码,他们必须是一个性质。

+0

为什么花时间来回答一个问题,但把这么小的努力成吗?你有没有看到Stack overflow提供的通常的高质量答案?你为什么会用你的单句回答来破坏这个答案,而这个答案也可能是评论? – Sheridan

0

你一定要试试下面menioned代码。

你有你的视图模型像

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

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

以声明的ObservableCollection你的镜头类应该是

[XmlInclude(typeof(Lens))] 
public class Lens 
{ 
    public string LensName { get; set; } 
    public string LensType { get; set; } 

}