0

Im puzzled

I have xml document

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

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>

And show document in a listbox

<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

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);
            }
        }

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>

Nothing is shown , any ideas.

klashagelqvist
  • 1,251
  • 2
  • 26
  • 52
  • Where are you actually calling the Bind() in code..? – MethodMan Jan 23 '12 at 14:23
  • I bind datacontext of usercontrol to viewmodel. If i put a breakpoint i can see that the viewmodel is loaded. – klashagelqvist Jan 23 '12 at 14:32
  • Is this your exact code? I only ask because the property assignment / notification stuff seems very messy (Reports = _provider is redundant for example, and event should be raised in the setter) – GazTheDestroyer Jan 23 '12 at 14:34
  • Thanks for your answer. I know about your code objections these are the results of my testing , but they have no impact on the functionality, so i left them in. I put in a debugconverter and i can see that the binding has the correct document loaded, but still nothing – klashagelqvist Jan 23 '12 at 14:42
  • Modified the code on suggestion of Mr Gaz – klashagelqvist Jan 23 '12 at 15:08

1 Answers1

2

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

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

     DataContext="{Binding Reports}"    
     ItemsSource="{Binding}"
SvenG
  • 5,155
  • 2
  • 27
  • 36
  • Thanks. Seems that you are right in your suggestion, it works when i set datacontext of listbox. But shouldnt ListBox get datacontext automatically from datacontext of Usercontrol. I have lots of samples with observablecollections where i do exactly the same as in question, and it works. Even stranger is that if i bind to a static xmldataprovider i can see the values in vs designer – klashagelqvist Jan 23 '12 at 15:18
  • Sorry I can't explain why this is the case, but it seems to be an internal issue in the XMLDataProvider, there are similar issues with binding the Source property:http://stackoverflow.com/questions/1866942/how-to-bind-xmldataprovider-source-to-mvvm-property So I guess its an internal problem in the .net Framework ... – SvenG Jan 23 '12 at 16:04