0
votes

I am new to WPF. In existing application,Combo box is not getting binding values from ObservableCollection. I have a class ShipmentItem. I need to bind combo box with WeightUnit field. below is the code:

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

Here is the observable collection :

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 is Listview, which has text box and combo box.Text box are getting their values from the binding path except ComboBox.And this is the XAML code for combo box.

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

Any help would be appreciated.

1
You need to check that the DataContext of the ComboBox is really an instance of ShipmentItem. Then your class ShipmentItem should implement at least INotifyPropertyChanged or inherit from DependencyObject. Hope that helps.PilouPili
I am displaying the ComboBox in the data template of a ListView item. The ComboBox is item bound to an observable collection. The ComboBox binding works well when it is NOT used in a ListView. However, when it is used in a ListView, the SelectedValue of the ComboBox does not bind correctly. The ComboBox does not display (or bind at all) the selected value when it loads.Tarun Mendiratta
I have a hard time grasping what you are willing to do. Try to do a mvce and provide model modelview and view.PilouPili
Do you see any binding errors on visual studio output window?Dipen Shah
No Dipen , there is not any errorTarun Mendiratta

1 Answers

0
votes

View

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

In the View class, set the DataContext to ViewModel

ViewModel

private ObservableCollection<ShipmentItem> _shipmentItemCollection;

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

continued (in a constructor or some method)

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