【问题标题】:WPF combobox does not show values at runtimeWPF 组合框在运行时不显示值
【发布时间】:2013-11-18 20:49:12
【问题描述】:

我是 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>

【问题讨论】:

    标签: wpf xaml binding combobox


    【解决方案1】:

    您应该将命名空间添加到根元素 (msdn)。

    注意:

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

    ...
    <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>
    ...
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    相关资源
    最近更新 更多