绑定到XMLDataProvider的TreeView - 在编辑器中显示数据,但在运行时显示为空

时间:2009-10-22 04:44:01

标签: wpf data-binding treeview

我已将TreeView绑定到XMLDataProvider。 TreeView在Visual Studio编辑器中按预期显示数据。但是当我按F5时,应用程序运行但树视图是空白的。有谁知道为什么我在运行应用程序时看不到它?

以下是整个代码:

<Window x:Class="TreeViewDataBinding.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
    <XmlDataProvider x:Key="FolderList">
        <x:XData>
            <TreeFolders>
                <Folder Name="Audit Reports" />
                <Folder Name="Joes Test" >
                    <Folder Name="Analysis01" />
                    <Folder Name="Test090803" />
                </Folder>
                <Folder Name="Carl" />
                <Folder Name="Steve" />
            </TreeFolders>

        </x:XData>
    </XmlDataProvider>
    <HierarchicalDataTemplate
                    x:Key="FolderTreeItemTemplate"
                    DataType="Folder">
        <HierarchicalDataTemplate.ItemsSource>
            <Binding XPath="child::*" />
        </HierarchicalDataTemplate.ItemsSource>
        <TextBlock Text="{Binding XPath=@Name}" />
    </HierarchicalDataTemplate>
        </Window.Resources>
    <Grid>
        <TreeView
            ItemsSource="{Binding Source={StaticResource FolderList}, XPath=//TreeFolders/*}"
            ItemTemplate="{StaticResource FolderTreeItemTemplate}" />
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:4)

我没有运行代码,但我的猜测是你需要指定命名空间:

xmlns=""

将xmlns附加到您的第一个数据标记:

<XmlDataProvider x:Key="FolderList">
    <x:XData>
        <TreeFolders xmlns="">
            <Folder Name="Audit Reports"/>
            <Folder Name="Joes Test">
                <Folder Name="Analysis01"/>
                <Folder Name="Test090803"/>
            </Folder>
            <Folder Name="Carl"/>
            <Folder Name="Steve"/>
        </TreeFolders>
    </x:XData>
</XmlDataProvider>