wpf combobox数据绑定问题

Gavin_Y 2012-05-22 11:26:24
我定义了两个基础类,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>

PelvicFractureInfos_left[0].FingerKeys1这个属性是KeyValue类的对象。它里面包含了ComboboxItemInfo的一个列表,我下拉框的数据就是绑定的这个列表里的数据,现在目前数据都取到了,就是在下拉框中数据显示不了。
高手帮忙看下。谢谢
...全文
761 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jaye-L 2012-09-10
  • 打赏
  • 举报
回复
WPF数据绑定这么麻烦的?
Gavin_Y 2012-05-25
  • 打赏
  • 举报
回复
问题解决,是绑定的数据源有问题,我本来绑定的是一个combobox绑定一个对象,对象里面有两个list的属性用来显示下拉数据和实际值。这样是不对的,之后我改成了,一个combobox绑定多个对象,而每个对象里面只有一个属性用了显示下拉数据和实际值,这样就可以了。
而且在绑定的时候直接写属性名称,不需要写binding
SelectedValuePath="ItemValueInfo" DisplayMemberPath="ItemDisylayInfo"
ohkuy 2012-05-22
  • 打赏
  • 举报
回复

我估计还是PelvicFractureInfos_left[0].FingerKeys1有问题
看下是不是实现IEnumerable接口,如List,数组等
test2050 2012-05-22
  • 打赏
  • 举报
回复
断点跟踪,注意value和display两个属性要对上号。
快溜 2012-05-22
  • 打赏
  • 举报
回复
public List<string> ItemValueInfo
{
get { return _itemValueInfo; }
set
{
_itemValueInfo = value;
NotifyPropertyChanged("ItemValueInfo");
}
}

Gavin_Y 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
我估计还是PelvicFractureInfos_left[0].FingerKeys1有问题
看下是不是实现IEnumerable接口,如List,数组等
[/Quote]
FingerKeys1 这个应该没有问题,因为我断点之后,里面的数据都是有的,FingerKeys1是一个列表数据,里面的每一项数据里面都有一个ComboBoxKeys的集合,ComboBoxKeys集合里面有两个list 用来保存combobox的显示值和实际值。

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧