View Code
private
childItem FindVisualChild
<
childItem
>
(DependencyObject obj)
where
childItem : DependencyObject
{
for ( int i = 0 ; i < VisualTreeHelper.GetChildrenCount(obj); i ++ )
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild < childItem > (child);
if (childOfChild != null )
return childOfChild;
}
}
return null ;
}
{
for ( int i = 0 ; i < VisualTreeHelper.GetChildrenCount(obj); i ++ )
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild < childItem > (child);
if (childOfChild != null )
return childOfChild;
}
}
return null ;
}
View Code
ListBox lbCurrent
=
e.Source
as
ListBox;
foreach ( object o in lbCurrent.Items)
{
ListBoxItem lbi = lbCurrent.ItemContainerGenerator.ContainerFromItem(o) as ListBoxItem;
ContentPresenter myContentPresenter = FindVisualChild < ContentPresenter > (lbi);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
ToggleButton tbhaha = myDataTemplate.FindName( " tbContainer " , myContentPresenter) as ToggleButton;
tbhaha.IsChecked = false ;
}
tb.IsChecked = true ;
foreach ( object o in lbCurrent.Items)
{
ListBoxItem lbi = lbCurrent.ItemContainerGenerator.ContainerFromItem(o) as ListBoxItem;
ContentPresenter myContentPresenter = FindVisualChild < ContentPresenter > (lbi);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
ToggleButton tbhaha = myDataTemplate.FindName( " tbContainer " , myContentPresenter) as ToggleButton;
tbhaha.IsChecked = false ;
}
tb.IsChecked = true ;