Link to home
Start Free TrialLog in
Avatar of suresh pondicherry
suresh pondicherryFlag for United States of America

asked on

Add menu item at run time in SL4

Hi,
I am using Silverlight 4 and VB.NET. I need to add the items into the menu at run time.
Consider i have button1, button2, button3. When i click buttons, the button name should be added to the menu in the tree structure.

Kind regards,
Pooja
Avatar of carlnorrbom
carlnorrbom
Flag of Sweden image

Hi,

Consider the following very simple example:
<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="600" d:DesignWidth="600" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:TreeView Height="200" HorizontalAlignment="Left" Margin="10,10,0,0" Name="TreeView1" VerticalAlignment="Top" Width="120" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="226,168,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="307,168,0,0" Name="Button2" VerticalAlignment="Top" Width="75" />
    </Grid>
</UserControl>

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        AddHandler Button1.Click, AddressOf AddMenuItems
        AddHandler Button2.Click, AddressOf AddMenuItems
    End Sub

    Private Sub AddMenuItems(ByVal sender As Object, ByVal e As EventArgs)
        Dim senderBtn As Button = CType(sender, Button)
        TreeView1.Items.Add(senderBtn.Name.ToString())
    End Sub
End Class

Open in new window


/Carl.
Avatar of suresh pondicherry

ASKER

Hi Carl,
You are great. I need to add these buttons under main parent say "Parent" in the tree structure.

It should appear like
1) Appears initially as
     Parent
2) When Button 1 is clicked
    Parent
            |___Button1
3)Parent
           |__Button1
           |__Button2

Also i need to assign the evenhandler for button1, buttons2 in the tree. That's it.

Kind regards,
Pooja
Hi,

Consider this example:
<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="600" d:DesignWidth="600" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:TreeView Height="200" HorizontalAlignment="Left" Margin="10,10,0,0" Name="TreeView1" VerticalAlignment="Top" Width="120">
            <sdk:TreeViewItem Header="Parent" />
        </sdk:TreeView>
        <Button Content="Button 1" Height="23" HorizontalAlignment="Left" Margin="136,12,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
        <Button Content="Button 2" Height="23" HorizontalAlignment="Left" Margin="230,12,0,0" Name="Button2" VerticalAlignment="Top" Width="75" />
        <TextBlock Text="Add Menu Items by clicking buttons" Name="TextBlock1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="136,35,0,0" />
    </Grid>
</UserControl>

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        AddHandler Button1.Click, AddressOf AddMenuItems
        AddHandler Button2.Click, AddressOf AddMenuItems
    End Sub

    Private Sub AddMenuItems(ByVal sender As Object, ByVal e As EventArgs)
        Dim senderBtn As Button = CType(sender, Button)
        Dim parentItem As TreeViewItem = TreeView1.Items(0)
        Dim childIteam As New TreeViewItem()
        childIteam.Header = senderBtn.Name.ToString()
        AddHandler childIteam.Selected, AddressOf ChildMenuItemSelected
        parentItem.Items.Add(childIteam)
    End Sub

    Private Sub ChildMenuItemSelected(ByVal sender As Object, ByVal e As EventArgs)
        Dim childItem As TreeViewItem = CType(sender, TreeViewItem)
        TextBlock1.Text = childItem.Header.ToString() & " was selected"
    End Sub
End Class

Open in new window


/Carl.
Hi Carl,
What i am using here is

 <StackPanel x:Name = "LayoutRoot" Background = "White">
                <Border BorderBrush = "Black" BorderThickness = "2">
                    <StackPanel>
                        <TextBlock Text = "TreeView Example" />
                        <sdk:TreeView Name="treeButtonView">
                            <sdk:TreeViewItem Header = "Other items containing TreeViewItem." Name="treeHeader">
                                <sdk:TreeViewItem.Items>
                                    <sdk:TreeViewItem Visibility="Collapsed" Name="treeItem1">
                                        <sdk:TreeViewItem.Header>
                                            <StackPanel Orientation = "Horizontal">
                                                <Image Height = "15" Width = "15" Source = "Images/Filter/ChildInformation.PNG" />
                                                <TextBlock Text = "Item 1" Margin = "2" />
                                            </StackPanel>
                                        </sdk:TreeViewItem.Header>
                                    </sdk:TreeViewItem>
                                    <sdk:TreeViewItem Visibility="Collapsed" Name="treeItem2">
                                        <sdk:TreeViewItem.Header>
                                            <StackPanel Orientation = "Horizontal">
                                                <Image Height = "15" Width = "15" Source ="Images/Filter/ChildInformation.PNG" />
                                                <TextBlock Text = "Item 2" Margin = "2" />
                                            </StackPanel>
                                        </sdk:TreeViewItem.Header>
                                    </sdk:TreeViewItem>
                                </sdk:TreeViewItem.Items>
                            </sdk:TreeViewItem>
                        </sdk:TreeView>
                    </StackPanel>
                </Border>
            </StackPanel>

I need to show Item1 when button 1 is clicked. Similarly i need to show Item 2 when button 2 is clicked. Also i need to attach the respective button clicks  event handlers to these Item1 and Item 2  respectively.
I need the solution from your code or from the code above. I am ok with any solutions. Please help me....

Kind regards,
Pooja
ASKER CERTIFIED SOLUTION
Avatar of carlnorrbom
carlnorrbom
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Carl,
I apologize for the silly doubt. I am new to silverlight .

Kind regards,
Pooja