Solved

Add menu item at run time in SL4

Posted on 2011-03-22
6
758 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now