ComboBox绑定到ObservableCollection不起作用

时间:2018-09-06 18:36:57

标签: wpf xaml data-binding combobox observablecollection

我是WPF的新手。在现有应用程序中,组合框未从ObservableCollection获取绑定值。我有一个ShipmentItem类。我需要将组合框与WeightUnit字段绑定。 下面是代码:

public partial class ShipmentItem : DataEntity {
    private int piecesField;
    private float weightField;
    private System.Nullable<float> widthField;
    private System.Nullable<float> lengthField;
    private System.Nullable<float> heightField;
    private string descriptionField;
    private WeightUnit weightUnitField;
    private LengthUnit lengthUnitField;

    public int Pieces {
        get {
            return this.piecesField;
        }
        set {
            this.piecesField = value;
            this.RaisePropertyChanged("Pieces");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    public float Weight {
        get {
            return this.weightField;
        }
        set {
            this.weightField = value;
            this.RaisePropertyChanged("Weight");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=2)]
    public System.Nullable<float> Width {
        get {
            return this.widthField;
        }
        set {
            this.widthField = value;
            this.RaisePropertyChanged("Width");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=3)]
    public System.Nullable<float> Length {
        get {
            return this.lengthField;
        }
        set {
            this.lengthField = value;
            this.RaisePropertyChanged("Length");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=4)]
    public System.Nullable<float> Height {
        get {
            return this.heightField;
        }
        set {
            this.heightField = value;
            this.RaisePropertyChanged("Height");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=5)]
    public string Description {
        get {
            return this.descriptionField;
        }
        set {
            this.descriptionField = value;
            this.RaisePropertyChanged("Description");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=6)]
    public WeightUnit WeightUnit {
        get {
            return this.weightUnitField;
        }
        set {
            this.weightUnitField = value;
            this.RaisePropertyChanged("WeightUnit");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=7)]
    public LengthUnit LengthUnit {
        get {
            return this.lengthUnitField;
        }
        set {
            this.lengthUnitField = value;
            this.RaisePropertyChanged("LengthUnit");
        }
    }}

这是可观察的集合:

public ObservableCollection<ShipmentItem> ShipmentItemCollection
    {
        get { return shipmentItemCollection; }
        set { shipmentItemCollection = (ObservableCollection<ShipmentItem>)value; }

    }
 shipmentItemCollection.Add(new ShipmentItem()
        {
            Weight = 0,
            Pieces = 0,
            WeightUnit = WeightUnit.Pounds,
            Description = string.Empty,
            Length = 0,
            Width = 0,
            Height = 0,
            LengthUnit = LengthUnit.Inches,
            Skidded = false,
            Stackable = false,
            Nmfc = string.Empty,
            FreightClass = string.Empty,
            DeliveryStop = 0
        });
 shipmentItemList.ItemsSource = shipmentItemCollection;
        shipmentItemList.DataContext = ShipmentItemCollection;

ShipmentItemList是Listview,它具有文本框和组合框。文本框从绑定路径中获取其值,ComboBox除外。这是组合框的XAML代码。

<ComboBox Name ="cmbWeightUnits" 
          SelectionChanged="cmbWeightUnits_SelectionChanged" 
          PreviewKeyDown="check_PreviewKeyDown" 
          ItemsSource="{Binding Path= ShipmentItemCollection}" 
          DisplayMemberPath="{Binding Path=WeightUnit}">
</ComboBox>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

查看

<ComboBox ItemsSource="{Binding ShipmentItemCollection}" 
          DisplayMemberPath="{Binding Path=WeightUnit}">
</ComboBox>

在“视图”类中,将DataContext设置为ViewModel

ViewModel

private ObservableCollection<ShipmentItem> _shipmentItemCollection;

public ObservableCollection<ShipmentItem> ShipmentItemCollection
{
    get { return _shipmentItemCollection; }
    set { _shipmentItemCollection = value; }
}

续(在构造函数或某种方法中)

ShipmentItemCollection.Add(new ShipmentItem()
{
    Weight = 0,
    Pieces = 0,
    WeightUnit = WeightUnit.Pounds,
    Description = string.Empty,
    Length = 0,
    Width = 0,
    Height = 0,
    LengthUnit = LengthUnit.Inches,
    Skidded = false,
    Stackable = false,
    Nmfc = string.Empty,
    FreightClass = string.Empty,
    DeliveryStop = 0
});