WPF组合框在运行时不显示值

WPF组合框在运行时不显示值,wpf,xaml,binding,combobox,Wpf,Xaml,Binding,Combobox,我是WPF的新手。 我有一个绑定到Window.Resources中定义的XML数据源的组合框 组合框值显示在设计器中,但在运行时为空 我是不是遗漏了什么 <Window x:Class="WpfExample4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.

我是WPF的新手。 我有一个绑定到Window.Resources中定义的XML数据源的组合框

组合框值显示在设计器中,但在运行时为空

我是不是遗漏了什么

    <Window x:Class="WpfExample4.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">
        <Window.Resources>
        <XmlDataProvider x:Key="xmlData">
            <x:XData>
                <customers>
                    <customer name="Customer 1">
                        <order desc="Big Order">
                            <orderDetail product="Glue" quantity="21" />
                            <orderDetail product="Fudge" quantity="32" />
                        </order>

                    </customer>
                    <customer name="Customer 2">
                        <order desc="First Order">
                            <orderDetail product="Mousetrap" quantity="4" />
                        </order>
                        </customer>

                    </customers>
            </x:XData>
            </XmlDataProvider>
        </Window.Resources>

            <Grid DataContext= "{Binding Source={StaticResource xmlData}, XPath=customers/customer}" Margin="4" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <!-- CUSTOMERS -->
                <DockPanel Grid.Row="0">
                    <TextBlock DockPanel.Dock="Top" FontWeight="Bold" Text="Customers" />
                    <ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" >
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding XPath=@name}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
                </DockPanel>
        </Grid>

    </Window>

您应该将名称空间添加到根元素()

注:

XML数据的根节点有一个xmlns属性,用于设置XML 将命名空间设置为空字符串。这是应用XPath的一个要求 查询到XAML页面中内联的数据岛。在这个 内联case、XAML以及数据岛继承了 Windows命名空间。因此,您需要设置 命名空间为空,以防止XPath查询被 Windows命名空间,这将误导查询

。。。
...

@user3006203所以你应该接受答案,阅读以下问题:
...
<Window.Resources>
    <XmlDataProvider x:Key="xmlData">
        <x:XData>
            <customers xmlns="">
                <customer name="Customer 1">
                    <order desc="Big Order">
                        <orderDetail product="Glue" quantity="21" />
                        <orderDetail product="Fudge" quantity="32" />
                    </order>
                </customer>
...