Silverlight ListBox Checkbox Selected Item

I have a Listbox that has a checkbox as part of an item template.

In even a checkbox click, I want to make sure the same list item is selected.

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

      

The main issue I am facing is that "ContainerFromElement" is not available in silverlight.

Any help is greatly appreciated.

Edit

This is the code I am running on a click event from 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 zero.

I have a list of layers that I want to turn on and off using checkboxes, I am open in a different way ...

+2


source to share


4 answers


In the selectionChanged event, you need to "traverse" the visual tree to find the checkbox. You can do this using VisualTreeHelper

This example shows what you need to do to navigate to the checkbox.

Below are some other solutions to this problem

You have to use RelativeSource binding between ListboxItem and CheckBox. The data item contains a checkbox. Change it so that it looks 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 in the CheckBox. This tutorial explains how with an example.

If you need more control, you should look at behaviors and triggers . They are a little more complex, but give you more control.

+5


source


Save the problem and create checkboxes, add them to the StackPanel in the Code Behind and then repeat them and just find the ones that have IsChecked.Value = True

. Took me for 2 minutes.



foreach(Object object in MyList){
    CheckBox cb1 = new CheckBox() { Content = object.MyProperty};
    MyStackPanel.Children.Add(cb1);
}

      

0


source


You can also have a collection, observable collection or hash set on the code line and set the check and uncheck method so you can add or remove selected objects it worked for me (I haven't tried the stack pane) ...

here's what i did.

first I have bound the element that I need to navigate to the CheckBox tag

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

      

then in the code behind i got the id whenever the checkbox was checked or unchecked and i am sure this item is shown because the same one i used to bind.

    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're done, you've already specified in the code the list of checked ...

0


source


For me - this code solves the goal.

<CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected, Mode=TwoWay}" >

      

0


source







All Articles