Solved

Add menu item at run time in SL4

Posted on 2011-03-22
6
760 Views
Last Modified: 2013-11-12
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
0
Comment
Question by:poojasureshkumar
  • 3
  • 3
6 Comments
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 35196838
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.
0
 

Author Comment

by:poojasureshkumar
ID: 35198664
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
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 35199384
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.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:poojasureshkumar
ID: 35199435
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
0
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 35199691
Hi,

If you need to "show" / expand a certain treeview item, why ask how to add one at runtime??? This is a completely different question than originally asked...

Also, what do you mean by adding the click event handlers from the buttons to the treeviewitems? They don't even have a click event...

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 ToggleMenuItemVisibility
        AddHandler Button2.Click, AddressOf ToggleMenuItemVisibility
    End Sub

    Private Sub ToggleMenuItemVisibility(ByVal sender As Object, ByVal e As EventArgs)
        Dim senderButton As Button = CType(sender, Button)
        If senderButton.Name = "Button1" Then
            treeItem1.Visibility = Windows.Visibility.Visible
            treeItem2.Visibility = Windows.Visibility.Collapsed
        Else
            treeItem1.Visibility = Windows.Visibility.Collapsed
            treeItem2.Visibility = Windows.Visibility.Visible
        End If
    End Sub

End Class

Open in new window


0
 

Author Comment

by:poojasureshkumar
ID: 35200069
Hi Carl,
I apologize for the silly doubt. I am new to silverlight .

Kind regards,
Pooja
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question