Listview仅显示XML中的一条记录

时间:2017-02-02 12:17:05

标签: c# xml wpf listview data-binding

好的。这是第一次,我有一个非常基本的问题。

我有一个外部XML存储过的地方。让我们说桌面。

XML看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>

-<Incidents>
    <Incident>    
        <IncidentNumber>4</IncidentNumber>    
        <Summary>4</Summary>    
        <Reason1>4</Reason1>    
        <Reason2>4</Reason2>    
        <Reason3>4</Reason3>    
    </Incident>    
</Incidents>

它有更多&#34;事件&#34; s,但我想缩短它。

在XAML中,我试图在ListView上显示这些数据,如下所示:

<ListView x:Name="lst_data" Height="290" Margin="10,29,10,0" VerticalAlignment="Top" Width="484" BorderThickness="1" BorderBrush="#FFDADADA" HorizontalAlignment="Center" ItemsSource="{Binding Source={StaticResource IncidentProvider}}">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath=//Incident/IncidentNumber}" Header="Number"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath=//Incident/Summary}" Header="Summary"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath=//Incident/Reason1}" Header="Reason 1"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath=//Incident/Reason2}" Header="Reason 2"/>
                    <GridViewColumn DisplayMemberBinding="{Binding XPath=//Incident/Reason3}" Header="Reason 3"/>
                </GridView>
            </ListView.View>
        </ListView>

这在某种程度上起作用,但只带来并显示第一条记录,而不显示其余记录。我尝试了在这个网站上找到的其他东西(比如把XML放在项目目录中或者使用Grid Listview上的DataContext驻留),没有用。

我做错了什么或错过了什么?

非常感谢你们抱歉这些基本问题。&lt;

编辑:根据超人的建议,现在就是这样:

Latest

2 个答案:

答案 0 :(得分:1)

以下是我接下来尝试的内容:

<强> MainWindows.xaml

<Window x:Class="WpfApplication1.MainWindow"
            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"
            xmlns:local="clr-namespace:WpfApplication1"
            xmlns:vm="clr-namespace:WpfApplication1"
            xmlns:model="clr-namespace:WpfApplication1"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="1200">
    <Window.Resources>
        <DataTemplate x:Key="IncidentTemplate" DataType="model:IncidentsIncident">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding IncidentNumber}"/>
                <TextBlock Text="{Binding Summary}"/>
                <TextBlock Text="{Binding Reason1}"/>
                <TextBlock Text="{Binding Reason2}"/>
                <TextBlock Text="{Binding Reason3}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <DataGrid x:Name="lst_data" AutoGenerateColumns="False" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemTemplate="{StaticResource IncidentTemplate}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Number" Binding="{Binding IncidentNumber}" Width="*"/>
                <DataGridTextColumn Header="Summary" Binding="{Binding Summary}" Width="*"/>
                <DataGridTextColumn Header="Reason 1" Binding="{Binding Reason1}" Width="*"/>
                <DataGridTextColumn Header="Reason 2" Binding="{Binding Reason2}" Width="*"/>
                <DataGridTextColumn Header="Reason 3" Binding="{Binding Reason3}" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

<强> MainWindows.xaml.cs

using System.Windows;
using System.Xml;
using System.Linq;
using System.Collections.Generic;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public List<IncidentsIncident> ItemCollection { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            XmlDocument doc = new XmlDocument();
            doc.Load("..\\..\\Incidents.xml");

            ItemCollection = new List<IncidentsIncident>();
            foreach (XmlElement item in doc.ChildNodes.OfType<XmlElement>().FirstOrDefault().ChildNodes)
            {
                ItemCollection.Add(new IncidentsIncident()
                {
                    IncidentNumber = byte.Parse(item.ChildNodes[0].InnerText),
                    Summary = byte.Parse(item.ChildNodes[1].InnerText),
                    Reason1 = byte.Parse(item.ChildNodes[2].InnerText),
                    Reason2 = byte.Parse(item.ChildNodes[3].InnerText),
                    Reason3 = byte.Parse(item.ChildNodes[4].InnerText)
                });
            }
            lst_data.ItemsSource = ItemCollection;
        }
    }
}

模特:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class IncidentsIncident
{

    private byte incidentNumberField;

    private byte summaryField;

    private byte reason1Field;

    private byte reason2Field;

    private byte reason3Field;

    /// <remarks/>
    public byte IncidentNumber
    {
        get
        {
            return this.incidentNumberField;
        }
        set
        {
            this.incidentNumberField = value;
        }
    }

    /// <remarks/>
    public byte Summary
    {
        get
        {
            return this.summaryField;
        }
        set
        {
            this.summaryField = value;
        }
    }

    /// <remarks/>
    public byte Reason1
    {
        get
        {
            return this.reason1Field;
        }
        set
        {
            this.reason1Field = value;
        }
    }

    /// <remarks/>
    public byte Reason2
    {
        get
        {
            return this.reason2Field;
        }
        set
        {
            this.reason2Field = value;
        }
    }

    /// <remarks/>
    public byte Reason3
    {
        get
        {
            return this.reason3Field;
        }
        set
        {
            this.reason3Field = value;
        }
    }
}

答案 1 :(得分:0)

如果你使用普通的Binding.Path应该绑定到DataContext对象的属性,该属性应该是XmlElement所以只需尝试{Binding Name}