- Back to Home »
- Windows Phone apps »
- Windows phone C# code to get selected data by selecting checkbox in listbox
Posted by :
Sudhir Chekuri
Saturday, 14 December 2013
Here is the example to get selected data by selecting checkbox in listbox:
<ListBox Height="600" Name="listBox1" Margin="0,0,0,0" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox x:Name="cb" Tag="{Binding PhoneNo}" IsChecked="{Binding IsSelected}" IsEnabled="True" Checked="cb_Checked_1" Unchecked="cb_Unchecked_1" Grid.Column="0" />
<TextBlock x:Name="Txt_Name" Grid.Column="1" Text="{Binding Name}" VerticalAlignment="Center"/>
<TextBlock x:Name="Txt_PhNo" Grid.Column="2" Text="{Binding PhoneNo}" VerticalAlignment="Center" />
<TextBlock Grid.Column="3" Text="{Binding Location}" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
string option_selected = "";
int check_count = 0;
private void cb_Checked_1(object sender, RoutedEventArgs e)
{
check_count = 0;
option_selected = "";
SearchElement(listBox1);
string s = " Total Selected Checkboxes : " + check_count.ToString()
+ Environment.NewLine + " Value Associated : " + option_selected;
}
private void cb_Unchecked_1(object sender, RoutedEventArgs e)
{
check_count = 0;
option_selected = "";
SearchElement(listBox1);
string s = " Total Selected Checkboxes : " + check_count.ToString()
+ Environment.NewLine + " Value Associated : " + option_selected;
}
private void SearchElement(DependencyObject targeted_control)
{
var count = VisualTreeHelper.GetChildrenCount(targeted_control); // targeted_control is the listbox
if (count > 0)
{
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targeted_control, i);
if (child is CheckBox) // specific/child control
{
CheckBox targeted_element = (CheckBox)child;
if (targeted_element.IsChecked == true)
{
if (option_selected != "")
{
option_selected = option_selected + " , " + targeted_element.Tag;
}
else
{
// get the value associated with the "checked" checkbox
option_selected = targeted_element.Tag.ToString();
}
// count the number of "Checked" checkboxes
check_count = check_count + 1;
return;
}
}
else
{
SearchElement(child);
}
}
}
else
{
return;
}
}
xaml code
<ListBox Height="600" Name="listBox1" Margin="0,0,0,0" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox x:Name="cb" Tag="{Binding PhoneNo}" IsChecked="{Binding IsSelected}" IsEnabled="True" Checked="cb_Checked_1" Unchecked="cb_Unchecked_1" Grid.Column="0" />
<TextBlock x:Name="Txt_Name" Grid.Column="1" Text="{Binding Name}" VerticalAlignment="Center"/>
<TextBlock x:Name="Txt_PhNo" Grid.Column="2" Text="{Binding PhoneNo}" VerticalAlignment="Center" />
<TextBlock Grid.Column="3" Text="{Binding Location}" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C# code:
string option_selected = "";
int check_count = 0;
private void cb_Checked_1(object sender, RoutedEventArgs e)
{
check_count = 0;
option_selected = "";
SearchElement(listBox1);
string s = " Total Selected Checkboxes : " + check_count.ToString()
+ Environment.NewLine + " Value Associated : " + option_selected;
}
private void cb_Unchecked_1(object sender, RoutedEventArgs e)
{
check_count = 0;
option_selected = "";
SearchElement(listBox1);
string s = " Total Selected Checkboxes : " + check_count.ToString()
+ Environment.NewLine + " Value Associated : " + option_selected;
}
private void SearchElement(DependencyObject targeted_control)
{
var count = VisualTreeHelper.GetChildrenCount(targeted_control); // targeted_control is the listbox
if (count > 0)
{
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targeted_control, i);
if (child is CheckBox) // specific/child control
{
CheckBox targeted_element = (CheckBox)child;
if (targeted_element.IsChecked == true)
{
if (option_selected != "")
{
option_selected = option_selected + " , " + targeted_element.Tag;
}
else
{
// get the value associated with the "checked" checkbox
option_selected = targeted_element.Tag.ToString();
}
// count the number of "Checked" checkboxes
check_count = check_count + 1;
return;
}
}
else
{
SearchElement(child);
}
}
}
else
{
return;
}
}
Thanks Sudhir for sharing . :-)
ReplyDelete