将字符串列表绑定到wpf列表视图返回BindingExpression路径错误
本文关键字:列表 返回 BindingExpression 路径 视图 错误 wpf 字符串 绑定 | 更新日期: 2023-09-27 17:57:33
我有两个字符串列表,声明如下:
Config.cs:
public class AgentSkills
{
public List<string> agentSkillsNameList=new List<string>();
public List<string> agentSkillsLvlList=new List<string>();
}
我从XML中检索字符串,并将它们添加到如下列表中:
Config.cs:
foreach (XmlNode skillNameNode in skillNameNodeList)
{
agentSkills.agentSkillsNameList.Add(skillNameNode.Attributes["value"].Value);
}
foreach (XmlNode skillLevelNode in skillLevelNodeList)
{
agentSkills.agentSkillsLvlList.Add(skillLevelNode.Attributes["value"].Value);
}
然后,我传递了这些列表,并将它们绑定到一个列表视图中,如下所示:
Info.cs:
Config.AgentSkills abc = new Config.AgentSkills();
this.AInfoLv.Items.Add(new { Label=" " + abc.agentSkillsNameList, Value=" " + abc.agentSkillsLvlList });
但它返回的错误如下:
System.Windows.Data Error:40:BindingExpression path error:'Value' property not found on'object'''<>f__AnonymousType0`1' (HashCode=2053256737)'. BindingExpression:Path=Value;
DataItem='<>f__AnonymousType0`1' (HashCode=2053256737);
target element is'TextBlock' (Name='');
target property is'Text' (type'String')
System.Windows.Data Error:40:BindingExpression path error:'Value' property not found on'object'''<>f__AnonymousType0`1' (HashCode=-861434965)'. BindingExpression:Path=Value;
DataItem='<>f__AnonymousType0`1' (HashCode=-861434965);
target element is'TextBlock' (Name='');
target property is'Text' (type'String')
System.Windows.Data Error:40:BindingExpression path error:'Value' property not found on'object'''<>f__AnonymousType0`1' (HashCode=1323488897)'. BindingExpression:Path=Value;
DataItem='<>f__AnonymousType0`1' (HashCode=1323488897);
target element is'TextBlock' (Name='');
target property is'Text' (type'String')
System.Windows.Data Error:40:BindingExpression path error:'Value' property not found on'object'''<>f__AnonymousType0`1' (HashCode=-2018970060)'. BindingExpression:Path=Value;
DataItem='<>f__AnonymousType0`1' (HashCode=-2018970060);
target element is'TextBlock' (Name='');
target property is'Text' (type'String')
System.Windows.Data Error:40:BindingExpression path error:'Value' property not found on'object'''<>f__AnonymousType0`1' (HashCode=249974195)'. BindingExpression:Path=Value;
DataItem='<>f__AnonymousType0`1' (HashCode=249974195);
target element is'TextBlock' (Name='');
target property is'Text' (type'String')
在wpf中将字符串列表绑定到列表视图中可以吗?如果不能,对这个问题有什么建议吗?
但您不是将字符串列表绑定到ListView
,而是将IEnumerable<String>
绑定到TextBlock.Text
字段,而它期望String
,正如您在错误中看到的那样。
解决问题的最快方法是将线路更改为
this.AInfoLv.Items.Add(new {
Label=" " + string.Join(", ", abc.agentSkillsNameList),
Value=" " + string.Join(", ", abc.agentSkillsLvlList)
});