show-notice
hide-notice

Wednesday, 17 July 2013

how to use ComboBox SelectionChanged event in silverlight


Introduction

I will explain how to use ComboBox SelectionChanged event in silverlight 
Example

<UserControl
      x:Class="SilverlightApps.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="800"
      xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
        >
      <Grid
x:Name
="LayoutRoot" Background="White">
          
<
Rectangle
                
Name="rectangle1"
                
Fill="AliceBlue"
                
HorizontalAlignment="Left"
                
Height="82"
                
Margin="20,12,0,0"
                
Stroke="Black"
                
VerticalAlignment="Top"
                
Width="600"
                
StrokeThickness="0"
              
/>
          
<
TextBlock
     
Height="82"
     
HorizontalAlignment="Left"
     
Margin="20,9,0,0"
     
Name="textBlock1"
     
Text="how to use ComboBox SelectionChanged event in silverlight"
     
VerticalAlignment="Top"
     
TextWrapping="Wrap"
     
FontSize="20"
     
FontStyle="Italic"
     
Foreground="MidnightBlue"
     
Width="600"
     
FontFamily="Comic Sans MS"
     
Padding="25,10,0,0"
         
     />
          
<
ComboBox
     
Height="23"
     
HorizontalAlignment="Left"
     
Margin="31,130,0,0"
     
Name="comboBox1"
     
VerticalAlignment="Top"
     
Width="188"
                
SelectionChanged="comboBox1_SelectionChanged"
              
>
                
<
ComboBoxItem
Content="Arabian Coffee"/>
                
<
ComboBoxItem
Content="Armadillo Fruit"/>
                
<
ComboBoxItem
Content="Australian Finger Lime"/>
                
<
ComboBoxItem
Content="Bahia Sunsapote"/>
                
<
ComboBoxItem
Content="Batoko Plum"/>
          
</
ComboBox>
          
<
TextBlock
                
Name="textBlock2"
                
HorizontalAlignment="Left"
                
Height="23"
                
Margin="265,130,0,0"
                
TextWrapping="Wrap"
                
Text="Select A ComboBox Item"
                
VerticalAlignment="Top"
                
Width="326"
                
FontSize="15"
                
Foreground="DeepPink"
              
/>
      </Grid>
</UserControl>
 

CS


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApps
{
    public partial class MainPage : UserControl
    {
       
public
MainPage()
        {
           
InitializeComponent();
        }
       
private
void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           
ComboBoxItem cbi = (sender as ComboBox).SelectedItem
as ComboBoxItem;
           
textBlock2.Text = "you selected: ";
           
textBlock2.Text += cbi.Content.ToString();
        }
    }
}
 

SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com