• WPF( MVVM)中TreeView 加载xml文档的方式及遇到的问题


    有如下xml代码:

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <FileSystem xmlns="">
     3   <Folder Name="Books">
     4     <Folder Name="Programming">
     5       <Folder Name="Windows">
     6         <Folder Name="WPF"/>
     7         <Folder Name="MFC"/>
     8         <Folder Name="Delphi"/>
     9       </Folder>
    10     </Folder>
    11     <Folder Name="Tools">
    12       <Folder Name="Development"/>
    13       <Folder Name="Designment"/>
    14       <Folder Name="Players"/>
    15     </Folder>
    16     <Folder Name="Books1">
    17       <Folder Name="Development"/>
    18       <Folder Name="Designment"/>
    19       <Folder Name="Players"/>
    20     </Folder>
    21     <Folder Name="Books2">
    22       <Folder Name="Development"/>
    23       <Folder Name="Designment"/>
    24       <Folder Name="Players"/>
    25     </Folder>
    26     <Folder Name="Books3">
    27       <Folder Name="Development"/>
    28       <Folder Name="Designment"/>
    29       <Folder Name="Players"/>
    30     </Folder>
    31   </Folder>
    32 </FileSystem>
    示例xml

    实现加载的方式有:

    1.在Xaml中定义一个静态的XmlDataProvider:如下:

     1  <UserControl.Resources>
     2 
     3     <XmlDataProvider x:Key="DicData"
     4                Source="../DesignData/report.xml"
     5                XPath="FileSystem/Folder" />
     6 
     7     <DataTemplate x:Key="dicItemTemplate">
     8         <Label Content="{Binding XPath=Name}"/>
     9     </DataTemplate>
    10 </UserControl.Resources>

    然后在TreeView ,ListBox等控件中使用:

    <ListBox x:Name="DicListBox" Margin="60,12,114,64" DockPanel.Dock="Left"
                 ItemsSource="{Binding 
             Source={StaticResource ReportData}}"
    
                 ItemTemplate="{StaticResource teamItemTemplate}"
                 IsSynchronizedWithCurrentItem="True"
                 Visibility="Visible" SelectionMode="Single">
    </ListBox>

    这样子是没有问题能正常显示。

    2.使用MVVM 数据绑定,直接加载xml文件

    ViewModel中代码:

    1 XmlDataProvider xmlDataProvider = new XmlDataProvider();
    2             xmlDataProvider.XPath = "FileSystem/Folder";
    3             XmlDocument xmlDocument = new XmlDocument();
    4             var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataContext\Setting\FileSystem.xml");
    5             xmlDocument.Load(path);
    6             xmlDataProvider.Document = xmlDocument;
    7             DicTypeData = xmlDataProvider;

    Xaml代码:在Binding的时候需要按照(

    DataContext="{Binding DicTypeData}" ItemsSource="{Binding}

    )的方式,才能加载出来数据,否则加载不出来。

    <TreeView Name="dicTypeTreeView" MinHeight="200" DataContext="{Binding DicTypeData}" ItemsSource="{Binding}">
                                <TreeView.ItemTemplate>
                                    <HierarchicalDataTemplate ItemsSource="{Binding XPath=Folder}">
                                        <StackPanel Orientation="Horizontal" Margin="2">
                                            <Image Source="pack://application:,,,/Sv.App;component/Image/Project/lq.png" Width="16" Height="16" SnapsToDevicePixels="True"></Image>
                                            <TextBlock Text="{Binding XPath=@Name}" Margin="5,0"></TextBlock>
                                        </StackPanel>
                                    </HierarchicalDataTemplate>
                                </TreeView.ItemTemplate>
                            </TreeView>

    3.直接在资源中,构建XmlDataProvider数据

    <XmlDataProvider x:Key="xdp" XPath="FileSystem/Folder">
                <x:XData>
                    <FileSystem xmlns="">
                        <Folder Name="Books">
                            <Folder Name="Programming">
                                <Folder Name="Windows">
                                    <Folder Name="WPF"/>
                                    <Folder Name="MFC"/>
                                    <Folder Name="Delphi"/>
                                </Folder>  
                            </Folder>
                            <Folder Name="Tools">
                                <Folder Name="Development"/>
                                <Folder Name="Designment"/>
                                <Folder Name="Players"/>
                            </Folder>
                        </Folder>
                    </FileSystem>
                </x:XData>
            </XmlDataProvider>
  • 相关阅读:
    url protocol
    wpf webbrowser取消js报错
    c#端口扫描器wpf+socket
    c#协变 抗变
    MTK刷机快捷键
    iTextCharp c#
    wince可用的7-zip
    直播平台搭建与相关资料
    pyinstall
    面向对象常见的术语
  • 原文地址:https://www.cnblogs.com/tianwen9579/p/11213058.html
Copyright © 2020-2023  润新知