在代码中加载XmlDataProvider [英] Load XmlDataProvider in code

查看:105
本文介绍了在代码中加载XmlDataProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑

我有xml文档

<?xml version="1.0" encoding="utf-8"?>
<Reports>
  <Report Id="AAAAA-ABBB">
    <DocId>10110001</DocId>
    <DocName>ESP North Casing</DocName>
    <DocType>2010-01-01</DocType>
    <Status>1</Status>
    <CreatedById>1</CreatedById>
    <SiteId>1</SiteId>
    <Language>1</Language>
    <Updated>2011-01-01</Updated>
    <Published>2011-01-01</Published>
    <FilePath>c:\\reports\20011001.docx</FilePath>
  </Report>
  <Report Id="AAAAA-ABBC">
    <DocId>10110002</DocId>
    <DocName>ESP South Casing</DocName>
    <DocType>2010-01-01</DocType>
    <Status>1</Status>
    <CreatedById>1</CreatedById>
    <SiteId>1</SiteId>
    <Language>1</Language>
    <Updated>2011-01-01</Updated>
    <Published>2011-01-01</Published>
    <FilePath>c:\\reports\20011001.docx</FilePath>
  </Report>
</Reports>

如果我定义了一个静态xmldataprovider之类的

If i define a static xmldataprovider like

 <UserControl.Resources>

    <XmlDataProvider x:Key="ReportData"
               Source="../DesignData/report.xml"
               XPath="Reports/Report" />

    <DataTemplate x:Key="teamItemTemplate">
        <Label Content="{Binding XPath=DocId}"/>
    </DataTemplate>
</UserControl.Resources>

并在列表框中显示文档

<ListBox x:Name="ReportListBox" Margin="60,12,114,64" DockPanel.Dock="Left"
             ItemsSource="{Binding 
         Source={StaticResource ReportData}}"

             ItemTemplate="{StaticResource teamItemTemplate}"
             IsSynchronizedWithCurrentItem="True"
             Visibility="Visible" SelectionMode="Single">
</ListBox>

我可以在列表框中看到数据

I can see the data in the listbox

如果我在代码中执行相同的操作,并在视图模型中加载xmldataprovider

If i do the same in code and load xmldataprovider in my viewmodel

  private XmlDataProvider GetXMLReports()
        {
              string filePath = Directory.GetCurrentDirectory() + @"\Data\report.xml";
              XmlDataProvider provider = new XmlDataProvider(); 
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 
              doc.Load(filePath); 
              provider.Document = doc; 
              provider.XPath = "Reports/Report";
              //Reports = _provider;

              return provider;

        }

        public XmlDataProvider Reports
        {
            get { return _provider; }
            set
            {
                _provider = value;
                this.RaisePropertyChanged(() => this.Reports);
            }
        }

如果我将datacontext设置为viewmodel并绑定到属性Report

If i set datacontext to viewmodel and bind to property Report

<ListBox x:Name="ReportListBox" Margin="60,12,114,64" DockPanel.Dock="Left"
                 ItemsSource="{Binding Path=Reports}"

                 ItemTemplate="{StaticResource teamItemTemplate}"
                 IsSynchronizedWithCurrentItem="True"
                 Visibility="Visible" SelectionMode="Single">
    </ListBox>

没有显示任何想法.

推荐答案

您要实现的目标不可能.

What you are trying to accomplish isn't possible afaik.

作为一种解决方法,您可以像这样在ListBox中设置DataContext,它应该可以工作:

As a workaround you can set the DataContext in the ListBox like this and it should work:

     DataContext="{Binding Reports}"    
     ItemsSource="{Binding}"

这篇关于在代码中加载XmlDataProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆