2011-10-08 48 views
0

我有一个XML文件,每个元素都有一个产品的名称和它的价格。如何以编程方式在XAML列表框中显示XML数据?

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

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

这里是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> 

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

你想用一个XmlDataProvider(解释here)。您需要将资源中的提供者,特定路径以获取要显示的数据,然后将您的ItemsSource绑定到资源

相关问题