2016-04-05 14 views
8

Tôi đã tìm kiếm rất nhiều và đã thử rất nhiều nhưng tôi không thể tìm hiểu lý do tại sao nó không hoạt động. Tôi đang cố gắng để xuất một tập tin XML vào một listview thông qua databinding trong xaml của tôi.C# Databinding một XML để một listview 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> 

Và tập tin XML của tôi trông như thế này

<?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> 

Dữ liệu không hiển thị trong listview. Tôi có cần thêm mã cs không?

+1

Mã của bạn có vẻ tốt với tôi, bạn có kiểm tra bất kỳ lỗi nào của DataBinding không? Giống như [so] (http://stackoverflow.com/questions/4026543/is-there-a-good-tool-for-debugging-xamls-databinding-behavior-errors-at-runti). – Kolky

+0

Tôi sẽ sử dụng treeview thay vì một listview. Xem ví dụ sau: http://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng

+3

Tôi vừa thử nghiệm nó và nó hoạt động như mong đợi. Tệp kunden.xml có tồn tại trong tài nguyên hoặc nội dung không? –

Trả lời

1

Không cần thêm mã cs.

Thuộc tính nguồn của XmlDataProvider là Uri, không phải là đường dẫn tệp. Vì vậy, nếu bạn viết chỉ có "kunden.xml", ứng dụng của bạn đang tìm kiếm tệp này trong tài nguyên ứng dụng. Để thêm tệp này vào tài nguyên ứng dụng, bạn sẽ thêm tệp xml vào dự án của mình (Add-> mục hiện có). Trong tập tin thuộc tính của nó thiết lập "Xây dựng hành động" thành "tài nguyên"

Nếu bạn muốn ứng dụng của bạn để tải từ tập tin độc lập (tức là kunden.xml được thực hiện cùng một thư mục nơi exe của bạn), bạn có trách nhiệm:

  • Sao chép xml vào thư mục đầu ra: thủ công hoặc tự động, tức là đặt kunden.xml "Build Action" thành "None", nhưng "Copy To Output Directory" thành "Copy if newer"
  • Change Source = ”kunden.xml ”To Source =" pack: // siteoforigin: ,,,/kunden.xml "

Nếu bạn muốn sử dụng abs tên olute của tệp, chỉ cần sử dụng Source = “file: /// D: /my/absolute/path/kunden.xml”.

Các vấn đề liên quan