Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

How do I get the selected checkboxes from this checkbox... RSS

5 replies

Last post Aug 02, 2009 05:58 PM by SteveWong

(0)
  • nbruckelmyer

    nbruckelmyer

    Member

    9 Points

    25 Posts

    How do I get the selected checkboxes from this checkbox list.

    Jul 27, 2009 04:12 PM | LINK

    I have a ListBox that contains checkboxes.  How do I get the selected checkboxes when I save the form.

    <ListBox x:Name="lbProxyOrganizations" Grid.Row="1" Grid.Column="1" Margin="0,3,0,3" Width="250">
       
    <ListBox.ItemTemplate>
           
    <DataTemplate>
               
    <CheckBox Content="{Binding Name}"/>
           
    </DataTemplate>
       
    </ListBox.ItemTemplate>
    </ListBox>

  • DJanjicek

    DJanjicek

    Participant

    1575 Points

    487 Posts

    Re: How do I get the selected checkboxes from this checkbox list.

    Jul 27, 2009 04:27 PM | LINK

    I guess the items in your binding collection you bind to he list have a boolean property? So you could query your collection for all items with the bool set to true

    var res = myCollection.Where(i => i.IsSelected);

    Something like that...

  • hageasilviu

    hageasilviu

    Member

    14 Points

    15 Posts

    Re: How do I get the selected checkboxes from this checkbox list.

    Jul 27, 2009 04:29 PM | LINK

    Follow this: http://silverlight.net/forums/p/12311/40426.aspx
    Please mark post as answer if they help you
  • SteveWong

    SteveWong

    Contributor

    6769 Points

    1351 Posts

    Re: Re: How do I get the selected checkboxes from this checkbox list.

    Jul 27, 2009 08:03 PM | LINK

    Just do like this

    _ListBox is the listbox where you put all the checkbox inside 

                var _CheckBoxList = from element in _ListBox.Items
                        where element is CheckBox
                        select element;
                foreach(CheckBox _ChkBox in _CheckBoxList)
                {
                    if (_ChkBox.IsChecked == true)
                    {
                        //Do what you want
                    }
                }

    Regards,
    SteveWong (HongKong)
    Please mark post as answer if they help you
  • nbruckelmyer

    nbruckelmyer

    Member

    9 Points

    25 Posts

    Re: Re: How do I get the selected checkboxes from this checkbox list.

    Jul 30, 2009 05:47 PM | LINK

    I tried your solution and its not working for me.  My _CheckBoxList is empty after the linq query and I do have checkboxes in my ListBox.  Is there something else I need to do.

    Thanks. 

  • SteveWong

    SteveWong

    Contributor

    6769 Points

    1351 Posts

    Re: Re: Re: How do I get the selected checkboxes from this checkbox list.

    Aug 02, 2009 05:58 PM | LINK

    I also cant get the result correctly, sorry for that I didnt check for the result before posting here

    btw, I have tried another workaround but dont know if you accept this or not

    it is to bind both content and IsChecked by a generic list with a new class called _Item which include two elements(Name and _IsChecked) For binding the IsChecked property of the checkbox, we have to use TwoWay Mode so to make everything simplier

    XAML CODE

                <ListBox x:Name="_ListBox" Grid.Row="1" Grid.Column="1" Height="250" Margin="0,3,0,3" Width="250">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding Path=Name}" IsChecked="{Binding Path=_IsChecked, Mode=TwoWay}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

    C# CODE

            List<_Item> _myStringList = new List<_Item>() {
                    new _Item(){Name = "test1", _IsChecked = false},
                    new _Item(){Name = "test2", _IsChecked = false},
                    new _Item(){Name = "test3", _IsChecked = false},
                    new _Item(){Name = "test4", _IsChecked = false},
                    new _Item(){Name = "test5", _IsChecked = false}
                };
            public Page()
            {
                InitializeComponent();
                Loaded += new RoutedEventHandler(Page_Loaded);
            }

            void Page_Loaded(object sender, RoutedEventArgs e)
            {           
                _ListBox.ItemsSource = _myStringList;
            }

     

    when you want to check which checkbox is checked, just check the _IsChecked of the _myStringList with an iteration

    Regards,
    SteveWong (HongKong)
    Please mark post as answer if they help you