有人能说出为什么这不管用吗。它确实很简单,但是启动时ListBox是空的。后面的代码只包含InitializeComponent(),没有其他内容。
希望有人有主意..。
<Window x:Class="DasDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="700">
<Window.Resources>
<XmlDataProvider x:Key="Maschinen" XPath="/machines">
<x:XData>
<machines>
<machine name="alte Maschine"/>
<machine name="neue Maschine"/>
</machines>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource Maschinen},XPath=machine/@name}"
IsSynchronizedWithCurrentItem="True"
SelectedIndex="1">
</ListBox>
</Window>@H.B.这是我测试的代码。启动它时,ListBox列表仍然是空的。我不知道出了什么问题。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.Resources>
<XmlDataProvider x:Key="Maschinen">
<x:XData>
<machines xmlns="">
<machine name="alte Maschine"/>
<machine name="neue Maschine"/>
</machines>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource Maschinen}, XPath=machine}"
IsSynchronizedWithCurrentItem="True" DisplayMemberPath="@name"
SelectedIndex="1">
</ListBox>
</StackPanel>
</Window> 发布于 2011-12-02 22:41:09
您需要将xmlns设置为空字符串:
<x:XData>
<machines xmlns="">
<machine name="alte Maschine"/>
<machine name="neue Maschine"/>
</machines>
</x:XData>MSDN
XML数据的根节点具有一个xmlns属性,该属性将XML命名空间设置为空字符串。这是将XPath查询应用于XAML页面内内联的数据岛的要求。在这种内联情况下,XAML以及数据岛继承了System.Windows命名空间。因此,您需要将名称空间设置为空白,以防止XPath查询被System.Windows命名空间限定,这会误导查询。
而且您可能想要这样绑定(即使它在结果方面没有什么不同):
<ListBox ItemsSource="{Binding Source={StaticResource Maschinen}, XPath=machine}"
IsSynchronizedWithCurrentItem="True" DisplayMemberPath="@name"
SelectedIndex="1">
</ListBox>https://stackoverflow.com/questions/8363572
复制相似问题