wpf combobox数据绑定问题
我定义了两个基础类,ComboboxItemInfo 是用来绑定下拉框数据的。下面是我的基础类和页面绑定的代码。
/// <summary> /// Combobox 下拉框数据 /// </summary> public class ComboboxItemInfo : INotifyPropertyChanged { private List<string> _itemDisplayInfo; /// <summary> /// 显示值 /// </summary> public List<string> ItemDisylayInfo { get { return _itemDisplayInfo; } set { if (_itemDisplayInfo != value) { _itemDisplayInfo = value; NotifyPropertyChanged("ItemDisylayInfo"); } } } private List<string> _itemValueInfo; /// <summary> /// 实际值 /// </summary> public List<string> ItemValueInfo { get { return _itemValueInfo; } set { if (_itemValueInfo != value) { _itemValueInfo = value; NotifyPropertyChanged("ItemValueInfo"); } } } //下拉框后面的textbox值 private string _itemTextBoxValue; /// <summary> /// Combobox后面的textbox值 /// </summary> public string ItemTextBoxValue { get { return _itemTextBoxValue; } set { if (_itemTextBoxValue != value) { _itemTextBoxValue = value; NotifyPropertyChanged("ItemTextBoxValue"); } } } //分组名称 private string _itemgroupName; public string ItemGroupName { get { return _itemgroupName; } set { if (_itemgroupName != value) { _itemgroupName = value; NotifyPropertyChanged("ItemGroupName"); } } } #region INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } /// <summary> /// /// </summary> public class KeyValue : INotifyPropertyChanged//, IDataErrorInfo { public int Index_Check = 0; private List<ComboboxItemInfo> _comboBoxKeys; /// <summary> /// 每个子项的下拉框数据 /// </summary> public List<ComboboxItemInfo> ComboBoxKeys { get { return _comboBoxKeys; } set { if (_comboBoxKeys != value) { _comboBoxKeys = value; NotifyPropertyChanged("ComboBoxKeys"); } } } private string _itemName; /// <summary> /// 每一项名称 /// </summary> public string ItemName { get { return _itemName; } set { if (_itemName != value) { _itemName = value; NotifyPropertyChanged("ItemName"); } } } //分组名称 private string _itemgroupName; public string ItemGroupName { get { return _itemgroupName; } set { if (_itemgroupName != value) { _itemgroupName = value; NotifyPropertyChanged("ItemGroupName"); } } } private Guid _evaluationItemId; public Guid EvaluationItemId { get { return _evaluationItemId; } set { if (_evaluationItemId != value) { _evaluationItemId = value; NotifyPropertyChanged("EvaluationItemId"); } } } private Visibility _isHaveText; /// <summary> /// 注明每一项后面是否包含textbox /// </summary> public Visibility IsHaveText { get { return _isHaveText; } set { if (_isHaveText != value) { _isHaveText = value; NotifyPropertyChanged("IsHaveText"); } } } private string _textInfo; /// <summary> /// textbox后面的文本信息 /// </summary> public string TextInfo { get { return _textInfo; } set { if (_textInfo != value) { _textInfo = value; NotifyPropertyChanged("TextInfo"); } } } #region INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion }
<ListBox ItemsSource="{Binding Path=PelvicFractureInfos_left[0].FingerKeys1, NotifyOnTargetUpdated=True, ValidatesOnDataErrors=True}"> <ListBox.Template> <ControlTemplate TargetType="{x:Type ListBox}"> <WrapPanel Orientation="Vertical" IsItemsHost="True"/> </ControlTemplate> </ListBox.Template> <ListBox.ItemTemplate> <DataTemplate > <StackPanel Orientation="Horizontal"> <Label Content="{Binding Path=ItemName}" Width="100" Height="25" ></Label> <ComboBox ItemsSource="{Binding Path=ComboBoxKeys[0], NotifyOnTargetUpdated=True, ValidatesOnDataErrors=True}" SelectedValuePath="{Binding Path=ComboBoxKeys[0].ItemValueInfo}" DisplayMemberPath="{Binding Path=ComboBoxKeys[0].ItemDisylayInfo}"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding ItemValueInfo}"/> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>