8

I have searched a lot and tried a lot but I cannot find out why it's not working. I am trying to output an XML file to a listview via databinding in my xaml.

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

And my XML file looks like this

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

The data is not showing in the listview. Do I need additional cs code?

Ilan
  • 2,762
  • 1
  • 13
  • 24
X-shunin
  • 81
  • 2
  • 1
    Your code seems fine to me, did you check for any DataBinding errors? Like [so](http://stackoverflow.com/questions/4026543/is-there-a-good-tool-for-debugging-xamls-databinding-behavior-errors-at-runti). – Kolky Apr 05 '16 at 10:54
  • I would use a treeview instead of a listview. See following example : http://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Apr 05 '16 at 11:01
  • 3
    I have just tested it and it is working as expected. Does the kunden.xml exist in the resource or content? – M'hand BOUGHIAS Apr 05 '16 at 11:11
  • Why all this extra stuff that does not apply to the question? – paparazzo Apr 05 '16 at 15:43
  • Well but my Listview doesn't show anything. Do I have to add the XML as a data source? I can only add SQL or something. Using VB 2015 Community – X-shunin Apr 06 '16 at 13:40

1 Answers1

1

No extra cs code is needed.

Source property of XmlDataProvider is a Uri, not a file path. So if you write there only “kunden.xml”, your application is looking for this file in application resources. To add this file to application resources you shall add your xml file to your project (Add->Existing item). In files its properties set "Build Action" to "Resource"

If you want your app to load from standalone file (i.e. kunden.xml shall be in the same folder where your exe is), you shall:

  • Copy xml to output folder: manually or automatically, i.e. set kunden.xml "Build Action" to "None", but "Copy To Output Directory" to "Copy if newer"
  • Change Source=”kunden.xml” to Source="pack://siteoforigin:,,,/kunden.xml"

If you want to use absolute name of the file, simply use Source=“file:///D:/my/absolute/path/kunden.xml”.

lexa
  • 414
  • 4
  • 5