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

时间:2011-10-08 05:03:09

标签: c# wpf xml xaml listbox

我有一个XML文件,每个元素都有产品名称和价格。

我有一个带有列表框的XAML文件。

如何在C#中以编程方式显示列表框中XML文件中的所有项目?感谢。

这是我的XML文件,包含3个示例产品:

<?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>

这是我的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>

1 个答案:

答案 0 :(得分:1)

您想使用XmlDataProvider(解释here)。您需要在Resources,speci路径中输出提供程序以获取要显示的数据,然后将ItemsSource绑定到资源