简体   繁体   English

将字符串列表绑定到WPF ListView返回BindingExpression路径错误

[英]Binding string lists into wpf listview returns BindingExpression path error

I've two string lists declared as below: 我有两个声明如下的字符串列表:

Config.cs: Config.cs:

public class AgentSkills 
{
  public List<string> agentSkillsNameList=new List<string>();
  public List<string> agentSkillsLvlList=new List<string>();
}

I retrieved the strings from an XML and added them to the lists as below: 我从XML中检索了字符串,并将它们添加到列表中,如下所示:

Config.cs: 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);
}

Then, I passed those list and bound them into a listview like this: 然后,我传递了这些列表并将它们绑定到这样的listview中:

Info.cs: Info.cs:

Config.AgentSkills abc = new Config.AgentSkills();

this.AInfoLv.Items.Add(new { Label=" " + abc.agentSkillsNameList, Value=" " + abc.agentSkillsLvlList });

But it returns errors as below: 但是它返回如下错误:

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')

Is it ok to bind string lists into a listview in wpf? 将字符串列表绑定到WPF中的列表视图中可以吗? If can't, is there any suggestion on this problem? 如果不能,对这个问题有什么建议吗?

But you aren't binding a list of strings to a ListView , you are binding an IEnumerable<String> to a TextBlock.Text field, when it is expecting a String as you can see in the errors. 但是,您没有将字符串列表绑定到ListView ,而是将IEnumerable<String>绑定到TextBlock.Text字段,这是因为您期望在错误中看到的String

The fastest way to solve your problem is to change the line to 解决问题的最快方法是将生产线更改为

this.AInfoLv.Items.Add(new { 
   Label=" " + string.Join(", ", abc.agentSkillsNameList), 
   Value=" " + string.Join(", ", abc.agentSkillsLvlList) 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM