Itzik Katzav

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



Re: Windows Presentation Foundation (WPF) Retrieve CheckBox Value from Listbox

Dennis Cheng-MSFT

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.

 






Re: Windows Presentation Foundation (WPF) Retrieve CheckBox Value from Listbox

Itzik Katzav

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>





Re: Windows Presentation Foundation (WPF) Retrieve CheckBox Value from Listbox

lee d

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




Re: Windows Presentation Foundation (WPF) Retrieve CheckBox Value from Listbox

Dennis Cheng - MSFT

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