【发布时间】:2019-02-12 02:02:08
【问题描述】:
我是 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>
任何帮助将不胜感激。
【问题讨论】:
-
你需要检查ComboBox的DataContext是否真的是ShipmentItem的一个实例。然后你的类 ShipmentItem 应该至少实现 INotifyPropertyChanged 或从 DependencyObject 继承。希望对您有所帮助。
-
我在 ListView 项的数据模板中显示 ComboBox。 ComboBox 是绑定到可观察集合的项目。 ComboBox 绑定在未在 ListView 中使用时效果很好。但是,在 ListView 中使用时,ComboBox 的 SelectedValue 无法正确绑定。 ComboBox 在加载时不显示(或根本不绑定)选定的值。
-
我很难理解你愿意做什么。尝试做一个mvce并提供模型modelview和视图。
-
您在 Visual Studio 输出窗口上看到任何绑定错误吗?
-
No Dipen ,没有任何错误
标签: wpf xaml data-binding combobox observablecollection