Access Checkbox inside list box in C# in Windows Phone
  • Hello
    I am developing a windows phone 8 application using C#. I have created check box inside list box in XAML. Following is the XAML code




    <ListBox Name="lstcontacts" ItemsSource="{Binding}" Margin="10,52,10,113" SelectionMode="Multiple" Foreground="#FF333333" >
    <ListBox.ItemTemplate>

    <DataTemplate>
    <StackPanel Orientation="Horizontal">
    <CheckBox Name="contactChk" IsChecked="true" Foreground="Black" Background="Black" BorderBrush="Black" />
    <TextBlock Name="txtResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>


    Please reply as earliest as possible

  • 3 Comments sorted by
  • Hello sajid. Please use following code to access check box inside listbox. 

    XML code 


     <ListBox Name="lstcontacts" ItemsSource="{Binding}" Margin="10,52,10,113"  SelectionMode="Multiple" Foreground="#FF333333" >
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel Orientation="Horizontal">
    <CheckBox x:Name="contactChk" Tag="{Binding}" IsChecked="true" IsEnabled="True" Foreground="Black" Background="Black" BorderBrush="Black" Checked="chbox_checked" />

    <TextBlock Name="txtResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />

    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
     
    C# Code 


     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Microsoft.Phone.Data;
    using Microsoft.Phone.UserData;
    using Microsoft.Phone.Tasks;
    using System.Data;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;


    namespace MyPhoneBackup
    {
    public partial class MyApp : PhoneApplicationPage
    {
    /

    public MyApp()
    {
    InitializeComponent();



    }

    /*







    private void Button_Click_1(object sender, RoutedEventArgs e)
    {

    SearchVisualTree(this.lstcontacts);




    ListBoxItem item = this.lstcontacts.ItemContainerGenerator.ContainerFromIndex(Convert.ToInt32(1)) as ListBoxItem;
    CheckBox contactChk = FindFirstElementInVisualTree<CheckBox>(item);
    contactChk.IsChecked = false;

    }

    /**/

    public T GetChild<T>(DependencyObject obj) where T : DependencyObject
    {
    DependencyObject child = null;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
    child = VisualTreeHelper.GetChild(obj, i);

    if (child != null && child.GetType() == typeof(T))
    break;

    else if (child != null)
    {
    child = GetChild<T>(child);

    if (child != null && child.GetType() == typeof(T))
    break;
    }
    }
    return child as T;
    }

    /**/

    private void SearchVisualTree(DependencyObject targetElement)
    {
    var count = VisualTreeHelper.GetChildrenCount(targetElement);
    if (count == 0)
    return;

    for (int i = 0; i < count; i++)
    {
    var child = VisualTreeHelper.GetChild(targetElement, i);
    if (child is TextBlock)
    {
    TextBlock myItems = (TextBlock)child;
    if (myItems.Text == "Item2") {
    myItems.Foreground = new SolidColorBrush(Colors.Green);
    return;
    }
    }
    else
    {
    SearchVisualTree(child);
    }
    }

    }

    private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
    {
    var count = VisualTreeHelper.GetChildrenCount(parentElement);
    if (count == 0)
    return null;
    for (int i = 0; i <= count; i++)
    {
    var child = VisualTreeHelper.GetChild(parentElement, i);
    if (child != null && child is T)
    {
    return (T)child;
    }
    else
    {
    var result = FindFirstElementInVisualTree<T>(child);
    if (result != null)
    return result;
    }
    }
    return null;
    }

    public void chbox_checked(object sender, RoutedEventArgs e) {



    }

    }



    }
     

    hussainroyal
  • Ok hussain, i will check this code and will give you feedback. Thanks for your reply.

  • i too will try it :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

Top Posters Weekly

Optimum Creative