【问题标题】:C# Databinding an XML to a listview WPFC# 将 XML 数据绑定到列表视图 WPF
【发布时间】:2016-04-05 10:49:52
【问题描述】:

我进行了很多搜索并尝试了很多,但我不知道为什么它不起作用。我正在尝试通过我的 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代码吗?

【问题讨论】:

  • 您的代码对我来说似乎很好,您是否检查过任何 DataBinding 错误?喜欢so
  • 我会使用树视图而不是列表视图。请参阅以下示例:stackoverflow.com/questions/28976601/…
  • 我刚刚对其进行了测试,它按预期工作。资源或内容中是否存在kunden.xml?
  • 为什么所有这些额外的东西都不适用于这个问题?
  • 好吧,但我的 Listview 没有显示任何内容。我必须将 XML 添加为数据源吗?我只能添加 SQL 什么的。使用 VB 2015 社区

标签: c# xml wpf data-binding


【解决方案1】:

不需要额外的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"。

【讨论】:

    猜你喜欢
    • 2016-02-24
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多