X-s*_*nin 8 c# xml data-binding wpf
我经常搜索并尝试了很多,但我无法找出它为什么不起作用.我试图通过我的xaml中的数据绑定将XML文件输出到listview.
<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>
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
列表视图中未显示数据.我需要额外的cs代码吗?
小智 1
不需要额外的cs代码。
\n\nXmlDataProvider 的 Source 属性是 Uri,而不是文件路径。因此,如果您只在那里写入 \xe2\x80\x9ckunden.xml\xe2\x80\x9d,则您的应用程序正在应用程序资源中查找此文件。要将此文件添加到应用程序资源,您应该将 xml 文件添加到项目中(添加->现有项目)。在文件中,其属性将“构建操作”设置为“资源”
\n\n如果您希望您的应用程序从独立文件加载(即 kunden.xml 应位于 exe 所在的同一文件夹中),您应该:
\n\n如果要使用文件的绝对名称,只需使用 Source=\xe2\x80\x9cfile:///D:/my/absolute/path/kunden.xml\xe2\x80\x9d 即可。
\n| 归档时间: |
|
| 查看次数: |
790 次 |
| 最近记录: |