Monday, October 29, 2012

Silverlight ListBox Checkbox Selected Item

Silverlight ListBox Checkbox Selected Item

I have a Listbox, that has a Checkbox as part of the item template.

In the click even of the Checkbox, I want to make sure that same list item is selected.

ListBoxItem lbi = ((ListBoxItem)listLayers.                        ContainerFromElement((CheckBox)sender)); lbi.IsSelected = true; 

The main issue that I'm having is that "ContainerFromElement" is not available in silverlight.

Any help is greatly appreciated.

Edit

This is the code I'm running in the click event of a checkbox inside my list:

MyObject obj = listLayers.SelectedItem as MyObject; obj.Visible = true; obj.Value = "50"; 

Using the RelativeSource binding on the checkboxes along with this code, I end up with obj equal to null.

I have a list of layers that I want to turn on and off via checkboxes, I'm open to another way...

Answers & Comments...

Answer: 1

In the selectionChanged event you have to "walk" the visual tree to find the checkbox. You can do this by using the VisualTreeHelper

This example show what you need to do to get to the checkbox.

Below are some other solutions to this problem

You should use a RelativeSource binding between the ListboxItem and the CheckBox. The datatemple contains a checkbox. Change it to look like this.

<CheckBox    IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent},      Path=IsSelected, Mode=TwoWay}" /> 

This creates a binding between the IsSelected property of the ListBox and the IsChecked property of the CheckBox. This tutorial explains how with an example.

If you need more control, you should have a look at behaviors and triggers. They're a bit more complex but give you you more control.

by : Sorskoothttp://stackoverflow.com/users/31722

Answer: 2

Save yourself the trouble and create the Checkboxes, add them to a StackPanel in Code Behind and then iterate them and just look for the ones that has IsChecked.Value = True. Took me 2 minutes.

foreach(Object object in MyList){     CheckBox cb1 = new CheckBox() { Content = object.MyProperty};     MyStackPanel.Children.Add(cb1); } 
by : Gunnar Fhttp://stackoverflow.com/users/574450

Answer: 3

You can also have a collection, observable collection or hash set on the codebehind and set a Method for the check and uncheck of the checkbox so you can add or remove the selected objects it worked out for me(I haven't tried the stack panel)...

here is what I did.

first I binded the Element I need to get to the tag of the CheckBox

<CheckBox Grid.Column="1" Margin="3" Height="50" Width="70" Visibility="Visible"      Tag="{Binding Id}" Checked="CheckBox_Checked"      Unchecked="CheckBox_Unchecked" /> 

then on the code behind I got the id whenever is checked or unchecked and Im sure that is the element shown because is the same I used on the binding.

    using System.Collections.ObjectModel;     private ObservableCollection<Guid> SelectedLocations = new ObservableCollection<Guid>();     private void CheckBox_Checked(object sender, RoutedEventArgs e)     {         Guid _guid = new Guid((((CheckBox)sender).Tag).ToString());         if (!SelectedLocations.Contains(_guid))         {             SelectedLocations.Add(_guid);         }                }      private void CheckBox_Unchecked(object sender, RoutedEventArgs e)     {         Guid _guid = new Guid((((CheckBox)sender).Tag).ToString());          if (SelectedLocations.Contains(_guid))         {             SelectedLocations.Remove(_guid);         }               } 

see, so when you are done you have already in the code a list of what's checked...

by : Garyhttp://stackoverflow.com/users/1067349




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog