可以';t在WPF中从我的XML向ListView添加项

本文关键字:XML 我的 ListView 添加 WPF 可以 | 更新日期: 2023-09-27 18:27:28

主要问题是这个。

我有两个XML,其中包含有关我的公司所做工作的信息。一个被认为是模板XML,您可以在其中找到常规信息,另一个是目录,其中包含有关每个单独设备的信息,包含对模板XML的引用。

它们看起来像这样目录XML

<list>
   <A>
      <B>
        <c>reference to template</c>
        <d>info 2</d>
        <e>info 3</e>
        <f>info 4</f>
        <g>
            <h>info5</h>
            <i>info5</i>
        </g>
      </B>
      <B>
        <c>reference to template</c>
        <d>info a</d>
        <e>info s</e>
        <f>info d</f>
        <g>
            <h>infof</h>
            <i>infog</i>
        </g>
      </B>
      <B>
        <c>reference to template</c>
        <d>info h</d>
        <e>info j</e>
        <f>info k</f>
        <g>
            <h>infot</h>
            <i>infoy</i>
        </g>
      </B>
   </A>
</list>

模板

<list>
   <R>
      <S>
        <t>info 7</t>
        <u>info 8</u>
        <v>info 9</v>
        <w>info 10</w>
      </S>
   </R>
</list>

我需要做的是显示listView中编目的所有设备,它将lits来自两个XML的信息。

我试过了,但没有成功,我只能显示一个设备,但它实际上没有显示,显示的只是不可见的信息。我使用以下方法运行这两个XML:

xDocument load = xDocument.load("Myxml.xml");
var run = (from x in load.Descendants("A")
           where x.Element("c").Value == comboBox1.SelectedItems.ToString()
           select new
           {
             a = x.Element("d").Valuye.ToString(),
             //here I gather the rest of the information
           }).ToList();
listView.Items.Add(run);
//after that I tried listview.Items.Add(run.a) ... but the code which I use to run through
//ends with FirstorDefault(), instead of ToList() and I try adding all the components manually

唯一出现的是一个不可见的设备,这意味着当我点击它时,我可以看到那里有东西,但我就是看不到信息。

所以我尝试使用相同的方法添加字符串,但得到了相同的结果。

有人能告诉我哪里错了吗?我看不见。

PS:在我设法做到这一点之后,我将实现一个功能,通过双击信息,客户端将能够更改信息。如果有人知道从哪里开始,请告诉我正确的方向

可以';t在WPF中从我的XML向ListView添加项

我认为您的linq查询需要一点修改,比如:

xDocument load = xDocument.load("Myxml.xml");
var run = (from x in load.Descendants("B")
       where x.Element("c") == comboBox1.SelectedItems.ToString()
       select new
       {
         a = x.Element("d").Valuye.ToString(),
         //here I gather the rest of the information
       }).ToList();

此外,您应该尝试在列表上使用for循环,并逐个添加字符串

foreach (var item in run)
    listView.Items.Add(item.a);

您可以在MSDN页面上查看Add方法的不同重载:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.listviewitemcollection.aspx

老实说,我不完全确定问题是什么。不过,我会尝试一下XDocument查询。

进行比较时,需要比较正确的类型。示例中的比较甚至不会编译,因为存在XElementString:的比较

where x.Element("c") == comboBox1.SelectedItems.ToString()

应为:

where x.Element("c").Value == comboBox1.SelectedItems.ToString()

如果总体目标是从查询中获得字符串列表,那么请查看以下内容:

string match = comboBox1.SelectedItems.ToString();
var doc = XDocument.Load( "MyXml.xml" );
var query = doc.Descendants( "B" )
    .Where( x => x.Element( "c" ).Value == match )
    .Select( x => x.Element( "d" ).Value )
    .ToList();

请注意,查询以"B"元素开头。从"A"元素开始将导致where子句中匹配0个元素。

此外,通过使用额外的变量来分解语句,即匹配条件的变量、XDocument的变量、查询的变量等,可以更容易地分解这些类型的问题。如果需要,甚至可以将查询分解为更多的子查询。

这应该会让你开始。

如果您使用列表视图的Detail模式,则需要将columns添加到列表中,并在每列对应的项目中添加subitems,否则您的项目将"不可见"。

有关更多详细信息,请参阅ListView.ColumnsListViewItem.SubItems成员。

我发现了问题所在,我需要添加一个可观察的集合,我在其中添加了我想放在视图列表中的内容,然后我在视图列表的可观察集合中放了信息。

感谢大家帮助我。