【问题标题】:Load XmlDataProvider in code在代码中加载 XmlDataProvider
【发布时间】:2012-01-23 13:55:44
【问题描述】:

我很困惑

我有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 像

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

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

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

  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

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

没有显示任何想法。

【问题讨论】:

  • 您实际上是在哪里调用代码中的 Bind() ..?
  • 我将用户控件的数据上下文绑定到视图模型。如果我放一个断点,我可以看到视图模型已加载。
  • 这是您的确切代码吗?我只是问,因为属性分配/通知的东西看起来很乱(例如 Reports = _provider 是多余的,应该在 setter 中引发事件)
  • 感谢您的回答。我知道您的代码反对意见这些是我的测试结果,但它们对功能没有影响,所以我把它们留在里面。我放入了一个调试转换器,我可以看到绑定加载了正确的文档,但仍然没有
  • 根据 Gaz 先生的建议修改了代码

标签: c# wpf xml xaml xmldataprovider


【解决方案1】:

你想要完成的事情是不可能的。

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

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

【讨论】:

  • 谢谢。似乎您的建议是正确的,当我设置列表框的数据上下文时它可以工作。但是 ListBox 不应该从 Usercontrol 的 datacontext 中自动获取 datacontext。我有很多带有 observablecollections 的样本,在这些样本中我所做的与所讨论的完全相同,并且它有效。更奇怪的是,如果我绑定到静态 xmldataprovider,我可以看到 vs 设计器中的值
  • 对不起,我无法解释为什么会这样,但这似乎是 XMLDataProvider 中的内部问题,绑定 Source 属性也存在类似问题:stackoverflow.com/questions/1866942/… 所以我猜它是.net 框架中的内部问题 ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多