简体   繁体   中英

WPF Binding ObservableCollection to ComboBox

I try to bind a list of Lens Objects and I would like to display the LensName property in my combobox.My lists in my code contain objects but comboboxes remain empty or the property doesn't display.I already tried all the ways known to bind my data without result.Thanks for helping

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>

Code Behind

      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;
        }

You need properties, not fields. These are fields:

public ObservableCollection<OphtalboxIA.Lens> RightBestlensList = new ObservableCollection<OphtalboxIA.Lens>();
public ObservableCollection<OphtalboxIA.Lens> LeftBestlensList = new ObservableCollection<OphtalboxIA.Lens>();

As properties, they would look like this:

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; }}

Furthermore, you have a typo in your binding: Source=LefttBestLensList . (One extra "t") And the casing is wrong ("...Lens..." vs. "...lens...").

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

You have to try below menioned code.

you have to Declare ObservableCollection in your ViewModel Like

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

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

Your Lens Class Should be

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM