ARTICLE

Adding Child Controls to a ComboBox Items in Silverlight using VB.NET

Posted by Mahesh Chand Articles | Silverlight using VB.NET January 08, 2011
This article demonstrates how to host various child controls within a ComboBox control in Silverlight using Visual Basic .NET.
Reader Level:

Adding Child Controls to a ComboBox

I am building a Project Survival Forecaster application in Silverlight 4 that forecasts the chances of surviving a software project. In this application, I need a ComboBox control to host other child controls.

This article demonstrates how to host various child controls within a ComboBox control in Silverlight.

Adding child controls to a ComboBox is similar to adding any item to a ComboBox. The ComboBox.Items represents a collection of items of a ComboBox. We can use ComboBox.Items.Add() method to add an object to a ComboBox. This object item can be a text, control or any other object.

The code snippet in Listing 1 adds a string, a Button, a TextBlock, a DateTime, a Rectangle, and a Panel with child controls to a ComboBox.

   Private Sub UserControl_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

 

        ' Add a String

        ComboBox1.Items.Add("ComboBox with Child Controls")

 

        ' Add a Button

        Dim btn As New Button()

        btn.Height = 50

        btn.Width = 150

        btn.Content = "Click ME"

        btn.Background = New SolidColorBrush(Colors.Orange)

        btn.Foreground = New SolidColorBrush(Colors.Black)

        ComboBox1.Items.Add(btn)

 

 

        ' Create a TextBlock and Add it to ComboBox

        Dim textBlockItem As New TextBlock()

        textBlockItem.TextAlignment = TextAlignment.Center

        textBlockItem.FontFamily = New FontFamily("Georgia")

        textBlockItem.FontSize = 14

        textBlockItem.FontWeight = FontWeights.ExtraBold

        textBlockItem.Text = "Hello! I am a text block."

        ' Add TextBlock to ComboBox

        ComboBox1.Items.Add(textBlockItem)

 

        ' Add a DateTime to a ComboBox

        Dim dateTime1 As New DateTime(2010, 10, 12, 8, 15, 55)

        ComboBox1.Items.Add(dateTime1)

 

        ' Add a Rectangle to a ComboBox

        Dim rect1 As New Rectangle()

        rect1.Width = 50

        rect1.Height = 50

        rect1.Fill = New SolidColorBrush(Colors.Red)

        ComboBox1.Items.Add(rect1)

 

        ' Add a panel that contains child controls

        Dim textBlock1 As New TextBlock()

        textBlock1.TextAlignment = TextAlignment.Center

        textBlock1.Text = "Panel with child controls"

 

        Dim ellipse1 As New Ellipse()

        ellipse1.Width = 50

        ellipse1.Height = 50

        ellipse1.Fill = New SolidColorBrush(Colors.Green)

 

        Dim comboBoxPanel As New StackPanel()

        comboBoxPanel.Width = 200

        comboBoxPanel.Background = New SolidColorBrush(Colors.Yellow)

        comboBoxPanel.Children.Add(textBlock1)

        comboBoxPanel.Children.Add(ellipse1)

 

        ' Add Panel to ComboBox

        ComboBox1.Items.Add(comboBoxPanel)

 

        ComboBox1.SelectedIndex = 0

        ComboBox1.Padding = New Thickness(0)

        ComboBox1.MaxWidth = 200

 

    End Sub

 

 

Listing 1

The output of Listing 1 looks like Figure 1.

ComboBoxchildcontrols.jpg
Figure 1


Further Readings

ComboBox control in Silverlight using VB.NET

ComboBox control in Silverlight using VB.NET

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor