簡體   English   中英

C#將XML數據綁定到列表視圖WPF

[英]C# Databinding an XML to a listview WPF

我已經搜索了很多並嘗試了很多,但是我無法找出為什么它不起作用。 我正在嘗試通過xaml中的數據綁定將XML文件輸出到列表視圖。

<Window
        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:Kundenstrom"
        xmlns:Properties="clr-namespace:Kundenstrom.Properties" x:Class="Kundenstrom.MainWindow"
        mc:Ignorable="d"
        Title="Kundenstrom" Height="232.5" Width="631" Icon="Hopstarter-Sleek-Xp-Basic-User-Group.ico">
    <Window.Resources>
        <XmlDataProvider x:Key="Kundenstromdaten" Source="kunden.xml" XPath="Kundenstrom/Kunden"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="11*"/>
            <ColumnDefinition Width="77*"/>
            <ColumnDefinition Width="11*"/>
            <ColumnDefinition Width="40*"/>
            <ColumnDefinition Width="21*"/>
            <ColumnDefinition Width="357*"/>
        </Grid.ColumnDefinitions>
        <TabControl x:Name="tabControl" Grid.ColumnSpan="6" TabStripPlacement="Top" Margin="10,0,10,10">
            <TabItem Header="Eintragen">
                <Grid Background="#FFE5E5E5">
                    <TextBox x:Name="txtGrund" Height="44" Margin="10,10,10,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
                    <ComboBox x:Name="cmbTyp1" HorizontalAlignment="Left" Margin="10,59,0,0" VerticalAlignment="Top" Width="287">
                        <ComboBoxItem Content="Laden"/>
                        <ComboBoxItem Content="Telefon"/>
                        <ComboBoxItem Content="Mail"/>
                    </ComboBox>
                    <ComboBox x:Name="cmbTyp2" Margin="302,58,10,0" VerticalAlignment="Top">
                        <ComboBoxItem Content="Anfrage"/>
                        <ComboBoxItem Content="Auftrag"/>
                        <ComboBoxItem Content="Abholung"/>
                    </ComboBox>
                    <Button x:Name="btnEintragen" Content="Eintragen" HorizontalAlignment="Left" Margin="10,86,0,0" VerticalAlignment="Top" Width="287" Height="36" Click="btnEintragen_Click"/>
                </Grid>
            </TabItem>
            <TabItem Header="Kunden anzeigen">
                <Grid Background="#FFE5E5E5">
                    <ListView Margin="10" ItemsSource="{Binding Source={StaticResource Kundenstromdaten}}">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn DisplayMemberBinding="{Binding XPath=Grund}" Header="Grund" />
                                <GridViewColumn DisplayMemberBinding="{Binding XPath=Typ1}" Header="Kundentyp" />
                                <GridViewColumn DisplayMemberBinding="{Binding XPath=Typ2}" Header="Anfragetyp" />
                                <GridViewColumn DisplayMemberBinding="{Binding XPath=Zeitpunkt}" Header="Zeitpunkt" />
                            </GridView>

                        </ListView.View>
                    </ListView>
                </Grid>
            </TabItem>
        </TabControl>

    </Grid>
</Window>

我的XML文件看起來像這樣

<?xml version="1.0" encoding="utf-8"?>
<Kundenstrom>
  <Kunden>
    <Grund>hfth</Grund>
    <Typ1>Laden</Typ1>
    <Typ2>Auftrag</Typ2>
    <Zeitpunkt>04.04.2016 15:01:38</Zeitpunkt>
  </Kunden>
  <Kunden>
    <Grund>testestsetsetse</Grund>
    <Typ1>Laden</Typ1>
    <Typ2>Anfrage</Typ2>
    <Zeitpunkt>04.04.2016 16:57:59</Zeitpunkt>
  </Kunden>
</Kundenstrom>

數據未顯示在列表視圖中。 我是否需要其他CS代碼?

無需額外的CS代碼。

XmlDataProvider的Source屬性是Uri,而不是文件路徑。 因此,如果僅在其中寫入“ kunden.xml”,則您的應用程序正在應用程序資源中尋找此文件。 要將這個文件添加到應用程序資源中,您應該將xml文件添加到您的項目中(添加->現有項)。 在文件的屬性中,將“構建操作”設置為“資源”

如果您希望您的應用程序從獨立文件加載(即kunden.xml應與exe文件位於同一文件夾中),則應:

  • 將xml復制到輸出文件夾:手動或自動,即將kunden.xml的“生成操作”設置為“無”,但將“復制到輸出目錄”設置為“如果更新則復制”
  • 將Source =” kunden.xml”更改為Source =“ pack:// siteoforigin:,, / kunden.xml”

如果要使用文件的絕對名稱,只需使用Source =“ file:/// D:/my/absolute/path/kunden.xml”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM