简体   繁体   English

无法从WPF中的XML向ListView添加项目

[英]Can't add items to ListView from my XMLs in WPF

The main problem is this one. 主要问题是这个。

I have 2 XMLs containing information about what my company does. 我有2个XML,其中包含有关我的公司工作的信息。 One is considered the template XML where you can find the general information and the other one is the Catalog containing information about each individual equipment, containing a reference to the template XML. 一种被认为是模板XML,您可以在其中找到常规信息,另一种是目录,其中包含有关每个设备的信息,其中包含对模板XML的引用。

They look like this Catalog 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>

Template 模板

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

What I need to do is to display all of the equipment catalogued in a listView, which will lits information from both XMLs. 我需要做的是显示在listView中分类的所有设备,这将同时显示来自两个XML的信息。

I've tried that and had no succes, all I can display is one equipment, weel it actually isn'y displayed, all that appears is invisible information. 我已经尝试过并且没有成功,我只能显示一种设备,实际上并没有显示出来,所有显示的都是看不见的信息。 I run through both XMLs using this: 我使用以下两种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

The only thing that Appears is an invisible Equipment, which means that when I click on it I can see there's something there, but I just can't see the information. 唯一出现的是一个看不见的设备,这意味着当我单击它时,我可以看到那里有东西,但是我看不到信息。

So I tried adding strings using the same methodology, but got the same result. 因此,我尝试使用相同的方法添加字符串,但得到的结果相同。

Can anyone please tell me where's my mistake? 谁能告诉我我的错误在哪里? I can't see it. 我看不到

PS: After I manage to do this, I'm gonna Implement a function, that by double clicking on the information, the client will be able to alter the information. PS:在我设法做到这一点之后,我将实现一个功能,即通过双击信息,客户端将能够更改信息。 If someone knows where to start with this one, please point me in the right direction 如果有人知道从此开始,请指出正确的方向

I believe your linq query needs a bit of a touch up, something like: 我相信您的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();

Also, you should try using a for loop on the list and adding the strings one by one 另外,您应该尝试在列表上使用for循环并逐个添加字符串

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

You can take a look at the different overloads of the Add method on this MSDN page: 您可以在此MSDN页面上查看Add方法的不同重载:

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

To be honest, I'm not entirely sure what the question is. 老实说,我不确定问题到底是什么。 I'll take a shot at the XDocument query, though. 不过,我将在XDocument查询中进行XDocument

When doing comparisons, you need to compare the correct types. 比较时,您需要比较正确的类型。 The comparison from the example won't even compile because there is a comparison of an XElement with a String : 该示例中的比较甚至无法编译,因为存在XElementString的比较:

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

Should be: 应该:

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

If the overall goal is to get a list of strings from the query, then take a look at the following: 如果总体目标是从查询中获取字符串列表,请查看以下内容:

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();

Notice that the query starts with the "B" element. 请注意,查询以“ B”元素开头。 Starting at the "A" element will result in 0 elements matched in the where clause. 从“ A”元素开始将导致where子句中的0个元素匹配。

Also, it is easier to break these types of problems down by using additional variables to break down the statement, ie, variables for the matching criteria, for the XDocument, for the query, etc... Even the query could be broken down into further sub-queries if needed. 同样,通过使用其他变量来分解语句,更容易解决这些类型的问题,例如,用于匹配条件,用于XDocument,用于查询等的变量。甚至可以将查询分解为如果需要,可以进行进一步的子查询。

This should get you started. 这应该使您入门。

If you are using the Detail mode of the list view, you need to add columns to the list and subitems in the items corresponding to each column, else your items will be "invisible". 如果您使用列表视图的“ Detail模式,则需要向列表中添加columns ,并在与每个列相对应的项目中添加subitems ,否则您的项目将是“不可见的”。

See the ListView.Columns and ListViewItem.SubItems members for more details. 有关更多详细信息,请参见ListView.ColumnsListViewItem.SubItems成员。

I found out what was wrong, I needed to add an observable collection, to which I added the content I wanted to put on the viewlist, and then I put the information in the observablecollection in the viewlist. 我发现了问题所在,我需要添加一个可观察的集合,在其中添加了要放入视图列表的内容,然后将信息放入了视图列表的observablecollection中。

Thank you all for helping me. 谢谢大家的帮助。

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

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