【问题标题】:WPF Binding ObservableCollection to ComboBoxWPF 将 ObservableCollection 绑定到 ComboBox
【发布时间】:2014-06-05 12:13:55
【问题描述】:

我尝试绑定镜头对象列表,我想在我的组合框中显示 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;
        }

【问题讨论】:

  • 绑定后是否填充了属性?如果是这样,那么原因是您的Lens 类没有实现INotifyPropertyChanged。这是将值推送到查看所必需的,在这种情况下,ComboBox 需要实现该接口,以便在值发生任何更改时得到通知,以便它可以将它们拉到查看。
  • 没有属性已经填充...然后我进行绑定...

标签: c# wpf xaml binding combobox


【解决方案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”)并且大小写错误(“...Lens...”与“...lens...”)。

【讨论】:

    【解决方案2】:

    RightBestlensListLeftBestlensList 必须在 ViewModel 类中,而不是在 Code Behind 中,并且它们必须是属性。

    【讨论】:

    • 为什么要花时间回答一个问题,却投入这么少的精力?您没有看到 Stack Overflow 提供的通常高质量的答案吗?为什么你会用你的单句答案来破坏它,这也可以是评论?
    【解决方案3】:

    你必须尝试下面提到的代码。

    你必须在你的 ViewModel 中声明 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; }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-18
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      相关资源
      最近更新 更多