none
Retrieve CheckBox Value from Listbox RRS feed

  • Question

  • Hello all

    I have a ListBox with DataTamplate which contains a checkbox (not binded) and a textblock (binded).

    I need window ok button click to retrieve each item CheckBox value.

    How can I do this?

    How can I even retrieve the CheckBox object from an Item?

    ListBox.ItemsIdea is actually a DataRowView because of the binding and not a ListBoxItem?

    Please only answer to me on how to retrieve the CheckBox value of an Item.

    Thanx Itzik

    Monday, January 8, 2007 9:38 PM

Answers

  • This technique also works on a ContentTemplate'ed ContentControl:

    1. Use ListBox's ItemContainerGenerator.ContainerFromIndex(int) or ItemContainerGenerator.ContainerFromItem(object) to get the ListBoxItem generated with your DataTemplate.
    2. Get the ContentPresenter from the ListBoxItem's ControlTemplate (visual tree) using VisualTreeHelper.GetChild(TheGeneratedListBoxItem, int)
    3. Use ContentPresenter.FindName(string, FrameworkTemplate) to get a reference of a named element from the DataTemplate.

     

    Wednesday, January 10, 2007 12:05 AM
  • Contentpresenter is wrapped in a border, so we have to get the border and then ContentPresenter. you are essentially doing the same thing as in the pseudo code
    Sunday, January 14, 2007 12:00 PM
    Moderator

All replies

  • This technique also works on a ContentTemplate'ed ContentControl:

    1. Use ListBox's ItemContainerGenerator.ContainerFromIndex(int) or ItemContainerGenerator.ContainerFromItem(object) to get the ListBoxItem generated with your DataTemplate.
    2. Get the ContentPresenter from the ListBoxItem's ControlTemplate (visual tree) using VisualTreeHelper.GetChild(TheGeneratedListBoxItem, int)
    3. Use ContentPresenter.FindName(string, FrameworkTemplate) to get a reference of a named element from the DataTemplate.

     

    Wednesday, January 10, 2007 12:05 AM
  • Hello again

    It works off course, but can you tell me why my code is so long and yours is three rows.

    Here is my code:

    ListBoxItem item = lbItems.ItemContainerGenerator.ContainerFromIndex(lbItems.SelectedIndex) as ListBoxItem;
    System.Windows.Controls.
    Border border = VisualTreeHelper.GetChild(item, 0) as System.Windows.Controls.Border;
    ContentPresenter pres = VisualTreeHelper.GetChild(border, 0) as ContentPresenter;
    Grid templateGrid = VisualTreeHelper.GetChild(pres, 0) as Grid;
    CheckBox checkBox = templateGrid.FindName("chk1") as CheckBox;
    MessageBox.Show(checkBox.IsChecked.ToString());

    in relevant to this Xaml code:
    <Window.Resources>
         <
    DataTemplate x:Key="GroupItemTemplate">
              <
    Grid>
                   <
    Grid.ColumnDefinitions>
                        <
    ColumnDefinition Width="20" />
                        <
    ColumnDefinition Width="*"/>
                   </
    Grid.ColumnDefinitions>
                   <
    CheckBox x:Name="chk1" IsChecked="{Binding Path=IsIsEnabled}" Grid.Column="0" />
                   <
    TextBlock Text="{Binding Path=NameName}" Grid.Column="1" FontWeight="Bold" />
              </
    Grid>
         </
    DataTemplate>
    </
    Window.Resources>

    <ListBox x:Name="lbItems" ItemTemplate="{StaticResource GroupItemTemplate}"
    ItemsSource="{Binding Path=Users}" IsSynchronizedWithCurrentItem="True">
    </ListBox>

    Saturday, January 13, 2007 11:25 AM
  • Contentpresenter is wrapped in a border, so we have to get the border and then ContentPresenter. you are essentially doing the same thing as in the pseudo code
    Sunday, January 14, 2007 12:00 PM
    Moderator
  • Thanks Lee! Yes, the ContentPresenter is somewhere in the ListBoxItem's ControlTemplate. For future reference, the SDK ships with the Styles and ControlTemplates that WPF uses to define each Control for each Windows theme so you can always use it as a guide: http://msdn2.microsoft.com/en-us/library/aa358533.aspx

    Monday, January 15, 2007 7:48 PM
  • Hi,
    Can you tell me how the same thing can be done in Silverlight v2.0? There doesn't seem to be a VisualTreeHelper in SL...
    thanks
    viktak
    Friday, May 30, 2008 6:28 PM
  • Hi,
    Can you tell me how the same thing can be done in Silverlight v2.0? There doesn't seem to be a VisualTreeHelper in SL...
    thanks
    viktak
    Friday, May 30, 2008 6:28 PM
  •   Hi,
    Can you tell me how the same thing can be done in Silverlight v2.0? There doesn't seem to be a VisualTreeHelper in SL...
    thanks
    viktak
    Friday, May 30, 2008 6:29 PM
  • Create list of comboxitem with its content as checkbox.

    Assign this collection to listbox datasource.

    this link provide a good example ListBox with checkbox

    Tuesday, November 30, 2010 3:09 PM