Solved

Add menu item at run time in SL4

Posted on 2011-03-22
6
761 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

860 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