(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">
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
Thanks, Todor, for the suggestion and sample code. I am trying to work with the sample code and:
1) I noticed that the sample code seems to be for WPF, not SL (XamMenu vs. XamWebMenu, etc.). This shouldn't be a problem as I'm converting the XamXXX references to XamWebXXX.
2) I can't seem to resolve HierarchicalDataTemplate. Can you tell me which SL assembly the HierarchicalDataTemplate type is in? It's apparently not in the XamWebMenu dll.
Thanks,
Tom