2016-04-05 8 views
8

私はたくさんの検索をしましたが、たくさん試しましたが、なぜ機能していないのかわかりません。私はxamlのデータバインディングを介してXMLファイルをリストビューに出力しようとしています。C#XMLをリストビューWPFへのデータバインディング

<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コードが必要ですか?

+1

あなたのコードは問題ないようですが、DataBindingエラーをチェックしましたか? [so](http://stackoverflow.com/questions/4026543/is-there-a-good-tool-for-debugging-xamls-databinding-behavior-errors-at-runti)のように。 – Kolky

+0

私はリストビューの代わりにツリービューを使用します。次の例を参照してください:http://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng

+3

私はちょうどそれをテストし、期待どおりに動作しています。 kunden.xmlはリソースまたはコンテンツに存在しますか? –

答えて

1

追加のcsコードは必要ありません。

XmlDataProviderのソースプロパティは、ファイルパスではなく、Uriです。したがって、 "kunden.xml"だけを記述すると、アプリケーションはアプリケーションリソース内でこのファイルを探しています。このファイルをアプリケーションリソースに追加するには、プロジェクトにXMLファイルを追加する必要があります([Add-> Existing item])。

:ファイルには、そのプロパティは、あなたのアプリが(すなわちkunden.xmlは、あなたのexeファイルが同じフォルダにしなければならない)、スタンドアロンのファイルからロードする場合、あなたは条「リソース」に

を「ビルドアクション」を設定します

  • xmlを出力フォルダにコピーする:kunden.xmlを "ビルドアクション"を "なし"に設定しますが、 "出力ディレクトリにコピー"を "それ以上コピーする"に設定します。
  • 変更ソース= "kunden.xml "source =" pack:// siteoforigin:,,,/kunden.xml "

absを使用する場合はファイル名を指定するには、単純にSource = "file:/// D:/my/absolute/path/kunden.xml"を使用します。

関連する問題