locked
HierarchicalDataTemplate In Code RRS feed

  • Question

  • I have a HierarchicalDataTemplate and XmlDataProvider in Xaml with a treeview bound to the XmlDataProvider, and what I would love to do, is modify the XPath that my treeview binding and HierarchicalDataTemplate are using.

    This is what it looks like at the moment:

    ....
        <XmlDataProvider x:Key="FamilyTree" XPath="*">
          <x:XData>
            <Root>
              <Object Name="Lisa" User="Jay">
                <Object Name="Mary" User="Jay"/>
                <Object Name="Dog" >
                  <Object Name="Bill" />
                </Object >
                <Object Name="Cat" />
                <Object Name="Ant">
                  <Object Name="Deer" />
                  <Object Name="Horse" />
                </Object>
                <Object Name="John" />
              </Object>
              <Object Name="Tom" User="Ward"/>
            </Root>
          </x:XData>
        </XmlDataProvider>
        <HierarchicalDataTemplate DataType="Object"
                                  ItemsSource="{Binding XPath=*[@User\=\'Jay\']}">
          <TextBlock Text="{Binding XPath=@Name}" />
        </HierarchicalDataTemplate>
      </Window.Resources>
      <Grid>

        <StackPanel>
          <Button Width="100" Height="50" Click="ChangeUser">Change User</Button>
          <TreeView Name="tree"
                    ItemsSource="{Binding Source={StaticResource FamilyTree}, XPath=*[@User\=\'Jay\']}"/>
        </StackPanel>
    ....

    I'm basically trying to make the XPath user based.

    Any ideas?
    Thanx!!
    -Jay
    Wednesday, June 13, 2007 2:30 PM

Answers

  • Hello, try this:

                  Binding binding = new Binding();

                  binding.XPath = "...";

                  binding.Source = this.Resources["FamilyTree"];

                  tree.SetBinding(TreeView.ItemsSourceProperty, binding);

    Friday, June 15, 2007 8:18 AM

All replies

  • Hello, try this:

                  Binding binding = new Binding();

                  binding.XPath = "...";

                  binding.Source = this.Resources["FamilyTree"];

                  tree.SetBinding(TreeView.ItemsSourceProperty, binding);

    Friday, June 15, 2007 8:18 AM
  • Thanx anyway, but that didn't work for me.  I do have a couple of alternatives I'm considering.

    Thursday, June 21, 2007 5:49 PM