Click to See Complete Forum and Search --> : Treeview Binding


AnuRShah
August 4th, 2009, 11:36 PM
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.

Arjay
August 5th, 2009, 01:19 AM
How are you assigning the ItemsSource property in the HierarchicalDataTemplate?

AnuRShah
August 5th, 2009, 09:36 AM
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>

Arjay
August 5th, 2009, 11:26 AM
Shouldn't


ItemsSource="{Binding Source={StaticResource productsXml}, XPath=ProductGroup}">


be a DynamicResource?


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

AnuRShah
August 5th, 2009, 12:53 PM
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.

Arjay
August 5th, 2009, 01:48 PM
Check out http://blogs.msdn.com/pedrosilva/archive/2009/05/04/programmatic-access-to-xmldataprovider.aspx


<XmlDataProvider x:Key="aboutProvider" XPath="ApplicationInfo" IsAsynchronous="False" IsInitialLoadEnabled="False">



XmlDataProvider provider = App.Current.TryFindResource("aboutProvider") as XmlDataProvider;

if (provider != null)
{
XmlDocument xmlDocAppInfo = new XmlDocument();
xmlDocAppInfo.Load("products.xml");
provider.Document = xmlDocAppInfo;
}

AnuRShah
August 5th, 2009, 03:46 PM
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

Arjay
August 5th, 2009, 03:53 PM
Go back to StaticResource.

AnuRShah
August 5th, 2009, 04:29 PM
That seemed to work.

Thanks a bunch.