Привязка ComboBox к ObservableCollection не работает - PullRequest
0 голосов
/ 06 сентября 2018

Я новичок в 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 голосов
/ 07 сентября 2018

View

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

В классе View установите 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
});
...