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
355
XamWebMenu: Programmatically building tree control for XamWebMenuItem
posted

(Note: I'm not sure why there are numerous blank lines in the resulting post -- in Preview mode -- as it looks fine in the 'Compose' editor.)

Using XAML, I am able to define a menu item (XamWebMenuItem) within a menu (XamWebMenu) such that the menu item displays a tree (XamWebTree) through the use of a control template.  (See the XAML code below.  I need to be able to dynamically build the tree at runtime, not at design time, but have been unable to figure out how to programmatically build the menu item tree (in C#) or at least be able to clear and populate the tree at runtime. 

If this can be accomplished, can you post some sample code?

 <igMenu:XamWebMenuItem x:Name="_menuItemProperties" Header="Property" Width="Auto" HorizontalAlignment="Right">

 

 

 

<igMenu:XamWebMenuItem Height="Auto" >

 

 

 

<igMenu:XamWebMenuItem.Template>

 

 

 

<ControlTemplate>

 

 

 

<igTree:XamWebTree>

 

 

 

<igTree:XamWebTreeItem Header="Sample" IsExpanded="True">

 

 

 

<igTree:XamWebTreeItem Header="SampleViewId" />

 

 

 

<igTree:XamWebTreeItem Header="SampleKey"/>

 

 

 

<igTree:XamWebTreeItem Header="LotNr"/>

 

 

 

<igTree:XamWebTreeItem Header="Race"/>

 

 

 

<igTree:XamWebTreeItem Header="Gender"/>

 

 

 

<igTree:XamWebTreeItem Header="Subject Age"/>

 

 

 

</igTree:XamWebTreeItem>

 

 

 

</igTree:XamWebTree>

 

 

 

</ControlTemplate>

 

 

 

</igMenu:XamWebMenuItem.Template>

 

 

 

</igMenu:XamWebMenuItem>

 

 

 

</igMenu:XamWebMenuItem>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Parents
  • 7922
    posted

    Hi TomKent

     

    One good solution I can think is to inherit XanMenuItem, add TreeItemsSource dependency property to it, and bind this property in control template of XamMenuItem. Below is a code sample how to do it.

      

    1.       Inherit from XamMenuItem, and declare your property

     

        public class XamMenuItemEx : XamMenuItem

        {

            #region TreeItemsSource

     

            /// <summary>

            /// Identifies the <see cref="TreeItemsSource"/> dependency property.

            /// </summary>

            public static readonly DependencyProperty TreeItemsSourceProperty = DependencyProperty.Register("TreeItemsSource", typeof(IEnumerable), typeof(XamMenuItemEx), null);

     

            public IEnumerable TreeItemsSource

            {

               get { return (IEnumerable)this.GetValue(TreeItemsSourceProperty); }

               set { this.SetValue(TreeItemsSourceProperty, value); }

            }

     

            #endregion // TreeItemsSource

             

        }

     

    2.       In control template bind XamTree.ItemsSource to above declared property

     

       <ig:XamMenu Grid.Row="1" Grid.Column="1" ExpandOnHover="False">

                <ig:XamMenuItem x:Name="_menuItemProperties" Header="Property" Width="Auto" HorizontalAlignment="Right">

                    <local:XamMenuItemEx Height="Auto"  StaysOpenOnClick="True" >

                        <local:XamMenuItemEx.Template>

                            <ControlTemplate TargetType="local:XamMenuItemEx">

                                <ig:XamTree x:Name="tree" ItemsSource="{TemplateBinding TreeItemsSource}">

                                    <ig:XamTree.HierarchicalItemTemplate>

                                        <ig:HierarchicalDataTemplate ItemsSource="{Binding Children}"/>

                                    </ig:XamTree.HierarchicalItemTemplate>

                                </ig:XamTree>

                            </ControlTemplate>

                        </local:XamMenuItemEx.Template>

                    </local:XamMenuItemEx>

                </ig:XamMenuItem>

     

                    By this way you can control the items in tree by this TreeItemsSource property.

     

    3.       So when you want to change the data displayed in tree you just change data set to TreeItemsSource property

     

     XamMenuItemEx item;

     private void ChangedData()

            {

                List<Data> data = new List<Data>();

                item.TreeItemsSource = data;

            }

     

    If you want to get reference to the tree you can do that in OnApplyTemplate method of XamMenuItemEx class

     

            XamTree tree

            public override void OnApplyTemplate()

            {

                tree = GetTemplateChild("tree") as XamTree;

            }

     

    Hope this helps

Reply Children