Question

I am trying to display some xml data in TreeView. Xml data is provided by ViewModel's property of type XmlDataProvider. However, I am unable to bind it with TreeView. I am using XmlDataProvider in XAML and unable to bind the ViewModel Property with XmlDataProvider in XAML. Below is code snippet:

public class MainViewModel : ViewModelBase {


    private static XmlDataProvider  xDoc;
    public static XmlDataProvider  XDoc {
      get {
        return xDoc;
      }
      set {
        xDoc = value;
        RaisePropertyChanged( "XDoc" );
      }

    }


    public MainViewModel( ) {
      Data d = new Data( );
      d.int1 = 12;
      d.int2 = 20;
      d.str = "Hello World";

      XmlSerializer serializer = new XmlSerializer( d.GetType( ) );
      StringWriter strWriter = new StringWriter( );
      serializer.Serialize( strWriter, d );
      XDoc = new XmlDataProvider { Document =XDocument.Parse( strWriter.ToString( ) ).ToXmlDocument (), XPath="child::node"} ;    
    }
  }

Xaml code:

<XmlDataProvider x:Key="xmlDP" Source="{ x:Static A:MainViewModel.XDoc}"  XPath="">
    </XmlDataProvider>
<TreeView Grid.Row="2" Grid.ColumnSpan="2" Name="xmlTree" 
       DataContext="{StaticResource xmlDP}" ItemsSource="{Binding}" ItemTemplate="{StaticResource treeViewTemplate}"/>

I am using mvvm light framework. When I try, I get exception that XDoc property should be static and I do not want to make it static.

If define my X:data inside XmlDataProvider within xaml file then treeview works:

 <XmlDataProvider x:Key="xmlDP" XPath="">
            <x:XData>
                <Employees>
                    <Employee Name="Steven Ballmer" DOB="1-Mar-1956">
                        <Title>CEO</Title>
                    </Employee>
                </Employees>
            </x:XData>
        </XmlDataProvider>

 <TreeView Grid.Row="2" Grid.ColumnSpan="2" Name="xmlTree" 
           DataContext="{StaticResource xmlDP}" ItemsSource="{Binding}" ItemTemplate="  {StaticResource treeViewTemplate}"/>
Was it helpful?

Solution

If I do not use XMLDataProvider and use only XMLDoxument then everything works as it should:

public class MainViewModel : ViewModelBase {


    private  XmlDocument  xDoc;
    public  XmlDocument  XDoc {
      get {
        return xDoc;
      }
      set {
        xDoc = value;
        RaisePropertyChanged( "XDoc" );
      }
    }

    public MainViewModel( ) {
      Data d = new Data( );
      d.int1 = 12;
      d.int2 = 20;
      d.str = "Hello World";


      XmlSerializer serializer = new XmlSerializer( d.GetType( ) );
      StringWriter strWriter = new StringWriter( );
      serializer.Serialize( strWriter, d );
      XDoc = XDocument.Parse( strWriter.ToString( ) ).ToXmlDocument () ;   
    }
  }

 <TreeView Grid.Row="2" Grid.ColumnSpan="2" Name="xmlTree" 
               ItemsSource="{Binding XDoc}" ItemTemplate="{StaticResource treeViewTemplate}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top