我想将我的数据从xml文件绑定到列表框但是我失败了。
这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<Cams>
<Cam>
<Name>Name</Name>
<Link>Link</Link>
<Path>Path</Path>
</Cam>
</Cams>
这是我的Xaml文件的开头:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Properties="clr-namespace:CamManager.Properties" x:Name="MainForm" x:Class="CamManager.MainWindow"
Title="CamManager" Height="190.69" Width="336.724" WindowStyle="ToolWindow" ResizeMode="NoResize" Background="White" BorderBrush="Black">
<Window.Resources>
<XmlDataProvider x:Key="Data" Source="CamCollection.xml"/>
</Window.Resources>
<Grid>
<TabControl HorizontalAlignment="Left" Height="322" VerticalAlignment="Top" Width="519">
<TabItem Header="Collection">
<Grid Background="#FFE5E5E5" Margin="0,0,185,153">
<ListBox x:Name="Collection" HorizontalAlignment="Left" Height="124" VerticalAlignment="Top" Width="235" ItemsSource="{Binding Source={StaticResource Data}, XPath=/Cams/Cam/Name}"/>
<Button x:Name="Launch" Content="Launch" HorizontalAlignment="Left" Margin="240,36,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.103,-0.084"/>
<Button x:Name="Delete" Content="Delete" HorizontalAlignment="Left" Margin="240,61,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</TabItem>
当我启动程序时,我的列表框保持空白,所以我希望有&#34;名称&#34;。
编辑:感谢您的解决方案,但这不是我的问题,我不知道为什么但是我必须在指定源时指定我的xml的完整路径
源=&#34; C:\用户... \ CamCollection.xml&#34; 如果你知道为什么?
答案 0 :(得分:2)
解决方法1:
<XmlDataProvider x:Key="Data" Source="CamCollection.xml" XPath="Cams/Cam/Name"/>
溶液2:
<ListBox x:Name="Collection" HorizontalAlignment="Left" Height="124" VerticalAlignment="Top" Width="235" ItemsSource="{Binding Source={StaticResource Data}, XPath=Cams/Cam/Name}"/>