In this example I will bind tables name from northwind database into a listbox and get an only those tables name which user will selected on listbox.
Step 1
Create a WPF Application.
Step 2
Add a listbox and button on window,it is look like this
<Grid> <ListBox Height="181" HorizontalAlignment="Left" Margin="179,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="159"/> <Button x:Name="btnOk" Height="23" Width="80" Margin="219,210,204,78" Content="OK" Click="btnOk_Click"></Button> </Grid>
Step 3
Create datatemplate for listbox,it is look like this
<Window.Resources> <DataTemplate x:Key="CheckListBox"> <StackPanel Orientation="Horizontal"> <CheckBox Name="ChkList" IsChecked="True" Content="{Binding Path=name}"></CheckBox> </StackPanel> </DataTemplate> </Window.Resources>
Data Template are a similar concept as Control Template. They give you a very flexible and powerful solution to replace the visual appearance of a data item in a control like ListBox, ComboBox or ListView.
Content Property - Bind the tables name in checkbox.Display tables name in checkbox.
Binding Path=name - name is represent the tables name of northwind database.it is column name of sys.tables
IsChecked Property - Gets or sets whether the checkbox is checked.
Step 4
Apply a datatemplate on listbox,it is look like this
<ListBox Height="181" HorizontalAlignment="Left" Margin="179,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="159" ItemTemplate="{StaticResource CheckListBox}" ItemsSource="{Binding}" />
ItemTemplate Property - Gets or sets the datatemplate used to display each item.
ItemSource Property- Gets or sets a collection used to generate the content of the itemsControl.
Step 5
Get tables name from northwind database.write a GetTableName method in code behind,it is look like this
/// <summary> /// Get table name from nortwind databases /// </summary> /// <returns>DataTable</returns> private DataTable GetTableName() { try { SqlConnection SqlCon = new SqlConnection(); SqlCon.ConnectionString = @"Data Source=shree\shree;Initial Catalog=Northwind;Integrated Security=True"; SqlCon.Open(); SqlCommand SqlComm = new SqlCommand("SELECT [name] FROM sys.tables"); SqlComm.Connection = SqlCon; DataTable Table = new DataTable(); SqlDataAdapter SqlDa = new SqlDataAdapter(); SqlDa.SelectCommand = SqlComm; SqlDa.Fill(Table); return Table; } catch (Exception ex) { throw new Exception(ex.Message); } }
Bind the tables name in listbox.
private void Window_Loaded(object sender, RoutedEventArgs e) { try { listBox1.DataContext = GetTableName(); // Bind Tables Name in ListBox } catch (Exception ex) { throw new Exception(ex.Message); } }
Step 6
Find child control from ContentPresenter.it is look like this
/// <summary> /// Find Child Control from ContentPresenter /// </summary> /// <typeparam name="ChildControl"></typeparam> /// <param name="DependencyObj"></param> /// <returns></returns> private ChildControl FindVisualChild<ChildControl>(DependencyObject DependencyObj) where ChildControl : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(DependencyObj); i++) { DependencyObject Child = VisualTreeHelper.GetChild(DependencyObj, i); if (Child != null && Child is ChildControl) { return (ChildControl)Child; } else { ChildControl ChildOfChild = FindVisualChild<ChildControl>(Child); if (ChildOfChild != null) { return ChildOfChild; } } } return null; }
Step 7
Get a selected checkbox items from listbox,it is look like this
/// <summary> /// Get a selected checkbox items /// </summary> private void GetSelectedCheckObjItem() { try { for (int i = 0; i < listBox1.Items.Count; i++) { // Get a all list items from listbox ListBoxItem ListBoxItemObj = (ListBoxItem)listBox1.ItemContainerGenerator.ContainerFromItem(listBox1.Items[i]); // find a ContentPresenter of that list item.. [Call FindVisualChild Method] ContentPresenter ContentPresenterObj = FindVisualChild<ContentPresenter>(ListBoxItemObj); // call FindName on the DataTemplate of that ContentPresenter DataTemplate DataTemplateObj = ContentPresenterObj.ContentTemplate; CheckBox Chk = (CheckBox)DataTemplateObj.FindName("ChkList", ContentPresenterObj); // get a selected checkbox items. if (Chk.IsChecked == true) { MessageBox.Show(Chk.Content.ToString().Trim()); } } } catch (Exception ex) { throw new Exception(ex.Message); } }
Call GetSelectedCheckObjItem method on button click event.
private void btnOk_Click(object sender, RoutedEventArgs e) { try { GetSelectedCheckObjItem(); // Get a selected checkbox items } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Full Code
1. XAML Code
<Window x:Class="WPF_CheckListBox.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> <Window.Resources> <DataTemplate x:Key="CheckListBox"> <StackPanel Orientation="Horizontal"> <CheckBox Name="ChkList" IsChecked="True" Content="{Binding Path=name}"></CheckBox> </StackPanel> </DataTemplate> </Window.Resources> <Grid> <ListBox Height="181" HorizontalAlignment="Left" Margin="179,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="159" ItemTemplate="{StaticResource CheckListBox}" ItemsSource="{Binding}" /> <Button x:Name="btnOk" Height="23" Width="80" Margin="219,210,204,78" Content="OK" Click="btnOk_Click"></Button> </Grid> </Window>
2. Code Behind
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.Navigation; using System.Windows.Shapes; using System.Data; using System.Data.SqlClient; namespace WPF_CheckListBox { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { try { listBox1.DataContext = GetTableName(); // Bind Tables Name in ListBox } catch (Exception ex) { throw new Exception(ex.Message); } } private void btnOk_Click(object sender, RoutedEventArgs e) { try { GetSelectedCheckObjItem(); // Get a selected checkbox items } catch (Exception ex) { MessageBox.Show(ex.Message); } } #region Methods /// <summary> /// Get table name from nortwind databases /// </summary> /// <returns>DataTable</returns> private DataTable GetTableName() { try { SqlConnection SqlCon = new SqlConnection(); SqlCon.ConnectionString = @"Data Source=shree\shree;Initial Catalog=Northwind;Integrated Security=True"; SqlCon.Open(); SqlCommand SqlComm = new SqlCommand("SELECT [name] FROM sys.tables"); SqlComm.Connection = SqlCon; DataTable Table = new DataTable(); SqlDataAdapter SqlDa = new SqlDataAdapter(); SqlDa.SelectCommand = SqlComm; SqlDa.Fill(Table); return Table; } catch (Exception ex) { throw new Exception(ex.Message); } } /// <summary> /// Find Child Control from ContentPresenter /// </summary> /// <typeparam name="ChildControl"></typeparam> /// <param name="DependencyObj"></param> /// <returns></returns> private ChildControl FindVisualChild<ChildControl>(DependencyObject DependencyObj) where ChildControl : DependencyObject { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(DependencyObj); i++) { DependencyObject Child = VisualTreeHelper.GetChild(DependencyObj, i); if (Child != null && Child is ChildControl) { return (ChildControl)Child; } else { ChildControl ChildOfChild = FindVisualChild<ChildControl>(Child); if (ChildOfChild != null) { return ChildOfChild; } } } return null; } /// <summary> /// Get a selected checkbox items /// </summary> private void GetSelectedCheckObjItem() { try { for (int i = 0; i < listBox1.Items.Count; i++) { // Get a all list items from listbox ListBoxItem ListBoxItemObj = (ListBoxItem)listBox1.ItemContainerGenerator.ContainerFromItem(listBox1.Items[i]); // find a ContentPresenter of that list item.. [Call FindVisualChild Method] ContentPresenter ContentPresenterObj = FindVisualChild<ContentPresenter>(ListBoxItemObj); // call FindName on the DataTemplate of that ContentPresenter DataTemplate DataTemplateObj = ContentPresenterObj.ContentTemplate; CheckBox Chk = (CheckBox)DataTemplateObj.FindName("ChkList", ContentPresenterObj); // get a selected checkbox items. if (Chk.IsChecked == true) { MessageBox.Show(Chk.Content.ToString().Trim()); } } } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion } }
Download
Download Source Code
Nice post! I really needed something like this! Thanks a lot!
ReplyDeleteYour good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteAWS Training in Bangalore
AWS training in sholinganallur
AWS training in Tambaram
AWS training in Velachery
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
ReplyDeleteAWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery
Thankyou for the valuable content.It was really helpful in understanding the concept.# BOOST Your GOOGLE RANKING.It’s Your Time To Be On #1st Page
ReplyDeleteOur Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
1000 Backlink at cheapest
50 High Quality for just 50 INR
2000 Backlink at cheapest
5000 Backlink at cheapest
I am really happy with your blog because your article is very unique and powerful for new.
ReplyDeleteSelenium Training in Pune
Data analyst generally works on creation of reports based on company’s data driven KPI’s(generally involves descriptive analytics), whereas Data scientists understand business and domain along with the technicalities to understand what will happen in future(more on descriptive + predictive analytics both)
ReplyDeleteEtlhive is a data science institute in pune. actuelly we wanted to promote our website on your site will you please contact me discus further details
website: - www.etlhive.com
contact: - +91 8055020011
https://bayanlarsitesi.com/
ReplyDeleteFiruzköy
Başıbüyük
Karadeniz
Taşdelen
O655
Konya
ReplyDeleteKayseri
Malatya
Elazığ
Tokat
1OEF
Adana
ReplyDeleteErzurum
Sinop
istanbul
Düzce
QHJSİ
Denizli
ReplyDeleteKonya
Denizli
ısparta
Bayburt
EO4
amasya
ReplyDeletesinop
çorum
sakarya
van
N8VFL
düzce
ReplyDeletesakarya
tunceli
van
bayburt
W0U
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
6RBD
Ağrı Lojistik
ReplyDeleteÇorlu Lojistik
Kars Lojistik
Antalya Lojistik
Rize Lojistik
K50PX
A6EAE
ReplyDeleteSilivri Parke Ustası
Ardahan Evden Eve Nakliyat
Çanakkale Evden Eve Nakliyat
Tunceli Evden Eve Nakliyat
Konya Evden Eve Nakliyat
7EDBF
ReplyDeleteÇerkezköy Marangoz
Bilecik Şehirler Arası Nakliyat
Gölbaşı Fayans Ustası
Bilecik Parça Eşya Taşıma
Ordu Lojistik
Silivri Boya Ustası
Konya Evden Eve Nakliyat
Tunceli Parça Eşya Taşıma
Ünye Evden Eve Nakliyat
B47CE
ReplyDelete%20 binance indirim kodu
55FD2
ReplyDeletemobil sohbet chat
mersin sesli mobil sohbet
hakkari canlı sohbet sitesi
Adana Sesli Görüntülü Sohbet
maraş bedava görüntülü sohbet
Amasya Ucretsiz Sohbet
Muş Sohbet Chat
Diyarbakır Goruntulu Sohbet
izmir kadınlarla sohbet et
48148
ReplyDeleteledger live
roninchain
quickswap
zkswap
trust wallet
avax
bitbox
dcent
arbitrum
5747FF2B4D
ReplyDeletemedi finance
emojicoin
moonbeam
tokenfi
emoji coin
rocketpool stake
mitosis
aethir
dymension