简体   繁体   English

如何以编程方式在XAML列表框中显示XML数据?

[英]How to display XML data in XAML listbox programmatically?

I have an XML file, each element has name of a product and it's price. 我有一个XML文件,每个元素都有一个产品名称和价格。

I have a XAML file with a listbox. 我有一个带有列表框的XAML文件。

How do I display all the items in the XML file in the listbox programmatically in C#? 如何在C#中以编程方式在列表框中显示XML文件中的所有项目? Thanks. 谢谢。

Here is my XML file with 3 sample products: 这是带有3个示例产品的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2011-09-20T15:04:12">
<Product>
<Name>Red Chair</Name>
<Price>29.5</Price>
</Product>
<Product>
<Name>Blue Chair</Name>
<Price>27</Price>
</Product>
<Product>
<Name>Round Table</Name>
<Price>31</Price>
</Product>
</dataroot>

Here's my XAML: 这是我的XAML:

<Window x:Class="DockPanel.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Inventory" Height="350" Width="525">
 <DockPanel>

    <ListBox Name="listBox1" Margin="10" >


    </ListBox>

</DockPanel>

You want to use an XmlDataProvider (explained here ). 您要使用XmlDataProvider( 在此处解释)。 You'll need to out the provider in your Resources, speci path to get the data you want displayed, and then bind your ItemsSource to the resource 您需要在资源中指定提供者,指定路径以获取要显示的数据,然后将ItemsSource绑定到资源

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

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