Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add menu item at run time in SL4

Posted on 2011-03-22
6
Medium Priority
?
769 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

721 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