Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1585
xamWebTree DataBind Programatically
posted

I am currently developing with you Silverlight 3 Controls and I was wondering how to databind a tree programatically... I have built a TreeNode Class that inherits from DependencyObject and has Children that are ObservableCollections.

Can I programmatically set my collection to the data context of a xamWebTree object and have it create a hierarchy tree with the ability to create as many sub levels as the object holds? I would rather do it programmatically instead of xaml, because in some cases, may have 1 tier of children, but in others I may have 3...

 

Something I was trying:

<code>

ObservableCollection<TreeViewNode> nodes = GetTreeViewData();

XamWebTree xamWebTree = new XamWebTree();

xamWebTree.DataContext = nodes;

Infragistics.Silverlight.Controls.HierarchicalDataTemplate ht = new Infragistics.Silverlight.Controls.HierarchicalDataTemplate();
ht.ItemsSource = new System.Windows.Data.Binding("ItemName");

xamWebTree.HierarchicalItemTemplate = ht;

 

</code>

  • 1585
    Verified Answer
    posted

    I built a xaml template that holds the max possibility of children that I will need.

    Then I set the ItemSource of my XamTree to this collection and with my Binding values set in my templates everything works perfectly. Since I will have a max of 3 Tiers of children, my template is built for that. With the object that I bind to, if it only has 2 Tiers, then it still will work, it just ignores the 3rd Tier since its null.

    Setting Item Source:

    ObservableCollection<TreeViewNode> nodes = provider.GetTreeViewData();
    xamTree.ItemSource =  nodes;

    My Templates:

    <igTree:XamTree ItemsSource="{Binding Path=Nodes}"
                       Margin="0,0"
                       Padding="0,0"
                       BorderBrush="#FF96B3DD"
                       Background="#FFFFFFFF"
                   
                       >
      <igTree:XamTree.HierarchicalItemTemplate>
        <ig:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}" >
          <DataTemplate>
            <StackPanel Orientation="Horizontal">
              <Image Source="{Binding ImageUrl}" />
              <HyperlinkButton
                      Content='{Binding ItemName}'
                      HorizontalAlignment='Left'
                      VerticalAlignment='Top'
                      Command="{Binding PostbackCommand}"
                      CommandParameter="{Binding GUID}"
                      Margin="2,0"
                      Foreground="Black"
                      IsTabStop="False">
              </HyperlinkButton>
            </StackPanel>
          </DataTemplate>
          <ig:HierarchicalDataTemplate.HierarchicalItemTemplate>
            <ig:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
              <!-- Second Level -->
              <DataTemplate>
                <StackPanel Orientation="Horizontal">
                  <Image Source="{Binding ImageUrl}" />
                  <HyperlinkButton
                          Content='{Binding ItemName}'
                          HorizontalAlignment='Left'
                          VerticalAlignment='Top'
                          Command="{Binding PostbackCommand}"
                          CommandParameter="{Binding GUID}"
                          Margin="2,0"
                          Foreground="Black"
                          IsTabStop="False">
                  </HyperlinkButton>
                </StackPanel>
              </DataTemplate>
              <ig:HierarchicalDataTemplate.HierarchicalItemTemplate>
                <ig:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                  <!-- Third Level -->
                  <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                      <Image Source="{Binding ImageUrl}" />
                      <HyperlinkButton
                              Content='{Binding ItemName}'
                              HorizontalAlignment='Left'
                              VerticalAlignment='Top'
                              Command="{Binding PostbackCommand}"
                              CommandParameter="{Binding GUID}"
                              Margin="2,0"
                              Foreground="Black"
                              IsTabStop="False">
                      </HyperlinkButton>
                    </StackPanel>
                  </DataTemplate>
                </ig:HierarchicalDataTemplate>
              </ig:HierarchicalDataTemplate.HierarchicalItemTemplate>
            </ig:HierarchicalDataTemplate>
          </ig:HierarchicalDataTemplate.HierarchicalItemTemplate>
        </ig:HierarchicalDataTemplate>
      </igTree:XamTree.HierarchicalItemTemplate>
    </igTree:XamTree>