Paul Martinez
Paul Martinez

Reputation: 562

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

Upvotes: 0

Views: 726

Answers (3)

Dhaval Patel
Dhaval Patel

Reputation: 7601

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

}

Upvotes: 0

user128300
user128300

Reputation:

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...").

Upvotes: 1

shfire
shfire

Reputation: 847

RightBestlensList and LeftBestlensList must be in ViewModel class and not in Code Behind and they must be a properties.

Upvotes: 0

Related Questions