简体   繁体   English

将XML文件显示到ListBox中

[英]Display XML file into ListBox

I'm new to WPF/C# programming. 我是WPF / C#编程的新手。 I'm trying to display a xml file content into a listbox using this XAML code : 我正在尝试使用此XAML代码将xml文件内容显示到列表框中:

<Window x:Class="test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <XmlDataProvider x:Key="HostsData"
                   Source="/Hosts.xml"
                   XPath="Hosts/Host" />
    </Window.Resources>
    <Grid>
        <ListBox Height="100" HorizontalAlignment="Left" Margin="98,70,0,0" Name="listBox1"
                 VerticalAlignment="Top" Width="120" SelectionChanged="listBox1_SelectionChanged"
                 ItemsSource="{Binding Source={StaticResource HostsData}}"
                 DisplayMemberPath="HostName"/>
    </Grid>
</Window>

And Hosts.xml contains : 并且Hosts.xml包含:

<Hosts>
  <Host>
    <IP>1.1.1.1</IP>
    <HostName>abc01</HostName>
  </Host>
  <Host>
    <IP>2.2.2.2</IP>
    <HostName>abc02</HostName>
  </Host>
</Hosts>

I build successfully but when I run the app, the listbox is empty ! 我构建成功,但是运行应用程序时,列表框为空! I've copied the Hosts.xml file everywhere but still nothing. 我已经在各处复制了Hosts.xml文件,但仍然没有复制任何内容。

Any idea please ? 有什么想法吗?

i think you forgot to specify a Data Context. 我认为您忘记了指定数据上下文。

DataContext="{Binding Source={StaticResource HostsData}}

either as a Grid xaml property, or Listbox. 作为Grid xaml属性或Listbox。

And since the data comes from a XML provider, try to use the Xpath property tag 并且由于数据来自XML提供程序,请尝试使用Xpath属性标签

EDIT : 编辑:

i found it more relevant to post a full example implementation, please ignore the Blend specific namespaces 我发现发布完整的示例实现更加相关,请忽略Blend特定的命名空间

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Screen_2_1_Name"
mc:Ignorable="d"
x:Class="WpfPrototype1Screens.Screen_2_1"
Width="640" Height="480">
<UserControl.Resources>
    <XmlDataProvider x:Key="uneDataSource" Source="http://www.lemonde.fr/rss/une.xml" d:IsDataSource="True"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource uneDataSource}}">
    <ListBox Margin="80,88,64,112" Style="{DynamicResource ListBox-Sketch}" ItemsSource="{Binding XPath=/rss/channel/item/title}"/>
</Grid>

i tried like this.... just have a look 我这样尝试过...。

<Window x:Class="WpfApplication2.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="300" Width="300">
<Window.Resources>
    <XmlDataProvider x:Key="BookmarkData" XPath="Hosts/Host">
        <x:XData>
            <Hosts>
                <Host>
                    <IP>1.1.1.1</IP>
                    <HostName>abc01</HostName>
                </Host>
                <Host>
                    <IP>2.2.2.2</IP>
                    <HostName>abc02</HostName>
                </Host>
            </Hosts>
        </x:XData>
    </XmlDataProvider>
</Window.Resources>
  <Grid>
    <ListBox
           Background="#999"
           BorderThickness="2"
           BorderBrush="White" 
           Margin="10"
           DisplayMemberPath="HostName" 
           ItemsSource="{Binding Source={StaticResource BookmarkData}, XPath=/Hosts/Host}"
           />

</Grid>

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

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