WPF ComboBox SelectionChanged – SelectedItem Method

  • by
WPF ComboBox SelectionChanged 001

WPF ComboBox SelectionChanged method helps us get the object selected by the users on the WPF Form/Page.

Method for WPF Combox SelectionChanged :

Object[] data = ((DataRowView)e.AddedItems[0]).Row.ItemArray;
txtBSelectedDay.Text = "You Selected : " + data[1].ToString() + " and its Id : " + data[0].ToString();

We get data in form of Array. We can then select the appropriate string/data as per the requirement.

WPF ComboBox SelectionChanged 001

Implementation :

WPF Design : Window1.xaml – 

<Window x:Class="WPFParallelCodes.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowState="Maximized">
<StackPanel Orientation="Vertical">

<TextBlock Margin="5" FontSize="20" HorizontalAlignment="Left">Select any day : </TextBlock>
<ComboBox Name="cbbDays" SelectionChanged="cbbDays_SelectionChanged" FontSize="20" Margin="5"
MaxWidth="250" Width="250" HorizontalAlignment="Left"/>
<Button Margin="5" Name="btnSubmit" Content="Submit" FontSize="20" MaxWidth="100" HorizontalAlignment="Left" Padding="2" Click="btnSubmit_Click_1"/>
<TextBlock Margin="5" FontSize="20" Foreground="BlueViolet" FontWeight="ExtraBlack" Name="txtBSelectedDay">Hi</TextBlock>
</StackPanel>
</Window>

WPF Code : Window1.xaml.cs –

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace WPFParallelCodes
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
FillComboBox();
}

public void FillComboBox()
{
try
{
String connectionString = "Data Source=192.168.0.192;Initial Catalog=ParallelCodes;User ID=sa;Password=789";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("select * from Producttbl", con);
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cbbDays.ItemsSource = dt.DefaultView;
cbbDays.DisplayMemberPath = "ProName";
cbbDays.SelectedValuePath = "Id";
cmd.Dispose();
con.Close();
}
catch (Exception ex)
{
}
}

private void btnSubmit_Click_1(object sender, RoutedEventArgs e)
{
txtBSelectedDay.Text = "You Selected : " + cbbDays.Text.ToString() + " and its Id : " + cbbDays.SelectedValue.ToString();
}

private void cbbDays_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

Object[] data = ((DataRowView)e.AddedItems[0]).Row.ItemArray;
txtBSelectedDay.Text = "You Selected : " + data[1].ToString() + " and its Id : " + data[0].ToString();
}
}
}

WPF ComboBox SelectionChanged 001

 

See Also :

WPF ComboBox from Database – Bind WPF ComboBox from SQL Database.,

WPF DataGrid from Database – Bind WPF DataGrid.

WPF Textbox Style – Change Background on Focus.

WPF Textbox with Rounded Corners – Design code.

 


  1. Base64Encode.io - Realtime Encode your string to Base64 format.
  2. Base64Decode.io - Realtime Decode Base64 data to plain text.
  3. Md5Hash.io - Realtime Encoding in Md5Hash.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.