要绑定的类型:
public class LocationRoad
{ public int ID { set; get; }
public string Code { set; get; }
public string Info { set; get; }
}
类的集合:
/// <summary> /// 当ComboBox选中项更改时发生 /// </summary> private LocationRoad _selectLocation; public LocationRoad SelectLocation { get { return this._selectLocation; } set { this._selectLocation = value; if (this.PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SelectLocation")); } } private ObservableCollection<LocationRoad> _locationRoad = null; public ObservableCollection<LocationRoad> LocationSource { get { if (this._locationRoad == null) { this._locationRoad = new ObservableCollection<LocationRoad>() { new LocationRoad() { ID = 1, Code = "NGQ", Info = "南岗区" }, new LocationRoad() { ID = 2, Code = "DLQ", Info = "道里区" }, new LocationRoad() { ID = 3, Code = "DWQ", Info = "道外区" }, new LocationRoad() { ID = 4, Code = "PFQ", Info = "平房区" }, new LocationRoad() { ID = 5, Code = "XFQ", Info = "香坊区" }, }; } return this._locationRoad; } set { this._locationRoad = value; if (this.PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("LocationSource")); } }
前台XAML文件绑定方式:
<ComboBox Margin="-16,3,0,5" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Name="cboxLocationKeyword"
ItemsSource="{Binding LocationSource,Mode=OneWay}"
SelectedValuePath="ID"
DisplayMemberPath="Info"
SelectedItem="{Binding SelectLocation}" />
原文地址:http://www.luacloud.com/2011/05/16/wpf%e4%b8%adcombobox%e6%95%b0%e6%8d%ae%e7%bb%91%e5%ae%9a%e6%96%b9%e6%b3%95/