Treeview Binding
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Treeview Binding

  1. #1
    Join Date
    Aug 2009
    Posts
    5

    Treeview Binding

    Hello,

    I have WPF aplication with a treeview that is bound to an XMLDataProvider in the XAML using HierarchicalDataTemplates. I'm trying to change the ItemsSource on the treeview at runtime because I don't know the xml filename until runtime.

    What's the best way to go about doing something like this? I've tried the following which doesn't work.

    XmlDataProvider dp = tree.FindResource("productsXml") as XmlDataProvider;
    dp.Source = new Uri("file://products.xml");


    Can you help me please? Thanks in advance.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,419

    Re: Treeview Binding

    How are you assigning the ItemsSource property in the HierarchicalDataTemplate?

  3. #3
    Join Date
    Aug 2009
    Posts
    5

    Re: Treeview Binding

    Here's my XAML code:

    <XmlDataProvider
    x:Key="productsXml"
    Source="TestConsole_Jim.xml"
    XPath="/PPCMSOfficeProductsStatus"
    />

    <HierarchicalDataTemplate
    DataType="ProductGroup"
    ItemsSource="{Binding XPath=ProductRegistryNode/Product}">
    <TextBlock Text="{Binding XPath=Title}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate
    DataType="Product"
    ItemsSource="{Binding XPath=Folder}">
    <TextBlock Text="{Binding XPath=Name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate
    DataType="SubFolder"
    ItemsSource="{Binding XPath=Folder}">
    <TextBlock Text="{Binding XPath=Name}" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate
    DataType="Folder"
    ItemsSource="{Binding XPath=Document}">
    <TextBlock Text="{Binding XPath=Name}" />
    </HierarchicalDataTemplate>

    <DataTemplate
    DataType="Document">
    <TextBlock Text="{Binding XPath=Name}" />
    </DataTemplate>
    </Grid.Resources>

    <TreeView Name="tree"
    Loaded="OnTreeLoaded"
    ItemsSource="{Binding Source={StaticResource productsXml}, XPath=ProductGroup}">
    </TreeView>

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,419

    Re: Treeview Binding

    Shouldn't

    Code:
     
    ItemsSource="{Binding Source={StaticResource productsXml}, XPath=ProductGroup}">
    be a DynamicResource?

    Code:
    ItemsSource="{Binding Source={DynamicResource productsXml}, XPath=ProductGroup}">

  5. #5
    Join Date
    Aug 2009
    Posts
    5

    Re: Treeview Binding

    When I change it to DynamicResource, I get the following exception.

    A 'DynamicResourceExtension' cannot be set on the 'Source' property of type 'Binding'. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,419

    Re: Treeview Binding

    Check out http://blogs.msdn.com/pedrosilva/arc...aprovider.aspx

    Code:
     
    <XmlDataProvider x:Key="aboutProvider" XPath="ApplicationInfo" IsAsynchronous="False" IsInitialLoadEnabled="False">
    Code:
    XmlDataProvider provider = App.Current.TryFindResource("aboutProvider") asXmlDataProvider;
     
    if (provider != null)
    {
      XmlDocument xmlDocAppInfo = newXmlDocument();
      xmlDocAppInfo.Load("products.xml");
      provider.Document = xmlDocAppInfo;
    }
    


  7. #7
    Join Date
    Aug 2009
    Posts
    5

    Re: Treeview Binding

    That didn't work; I still get the same error:

    A 'DynamicResourceExtension' cannot be set on the 'Source' property of type 'Binding'. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject.

    I've attached my XAML & .cs files
    Attached Files Attached Files

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,419

    Re: Treeview Binding

    Go back to StaticResource.

  9. #9
    Join Date
    Aug 2009
    Posts
    5

    Re: Treeview Binding

    That seemed to work.

    Thanks a bunch.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center