I posted this question in a thread of some other (related) question, but no suggestion yet.
I was looking for how to get the real control out of the ContentPresenter in the ItemsControl, but couldn't find a good way.
(one of my case is to remove all of the eventhandlers when this ItemsControl is removed from it's visual parent's DOM.
Lets say there are tens of data objects in the collection and everyone is displayed through the control hosted inside the Datatemplate. When the instance of the control in DataTemplate is constructed, some event hadlers is attached. So when remove the ItemsControl
from its parent, I want to get the individal control (hosted in the DataTemplate) to let it remove those event handlers).
There are a couple of other cases I need to get hold of the individual controls contained in the ItemsControl through the DataTemplate.
The simplified snippet shows the idea, There is a DateTemplate wich is used for the ItemsControl. I could get the StackPanel I put in the ItemsControl.ItemPanel. But the children of it are ContentPresentor. How do I convert or find the real control specified
in the DateTemplate?
Maybe build my onwcontrol to replace rhe ItemsControl is the only way?
rich_r
Member
17 Points
29 Posts
how to get the control (hosted inside the DataTemplate) out from the ItemsControl
Sep 23, 2008 08:07 PM | LINK
I posted this question in a thread of some other (related) question, but no suggestion yet.
I was looking for how to get the real control out of the ContentPresenter in the ItemsControl, but couldn't find a good way.
(one of my case is to remove all of the eventhandlers when this ItemsControl is removed from it's visual parent's DOM.
Lets say there are tens of data objects in the collection and everyone is displayed through the control hosted inside the Datatemplate. When the instance of the control in DataTemplate is constructed, some event hadlers is attached. So when remove the ItemsControl from its parent, I want to get the individal control (hosted in the DataTemplate) to let it remove those event handlers).
There are a couple of other cases I need to get hold of the individual controls contained in the ItemsControl through the DataTemplate.
The simplified snippet shows the idea, There is a DateTemplate wich is used for the ItemsControl. I could get the StackPanel I put in the ItemsControl.ItemPanel. But the children of it are ContentPresentor. How do I convert or find the real control specified in the DateTemplate?
Maybe build my onwcontrol to replace rhe ItemsControl is the only way?
Thanks for the help!
<DataTemplate x:Key="Item_Template">
<Grid Margin="3" HorizontalAlignment="Center" >
<mycontrols:DataTemplateMedia IsSelected="false" Source="{Binding SourceData}" />
</Grid>
</DataTemplate>
DataTemplateMedia is another control which hosts MediaElement/Image based one the source.
<ItemsControl x:Name="Container" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel x:Name="Container_StackPanel" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
-------------------------------
StackPanel st = GetItemsControlStackPanel(Container, "Container_StackPanel");
int count = st.Children.Count;
for (int i = 0; i < count; i++)
);
{
FrameworkElement el = (FrameworkElement)(st.Children
//el is ContentPresenter <===================== how to get/conver to the DataTemplateMedia
}
--------------------------------
public StackPanel GetItemsControlStackPanel(DependencyObject parent, string sChildName)
{
DependencyObject child = null;
for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
child = VisualTreeHelper.GetChild(parent, i) as DependencyObject;
if (child != null && (child.GetType() == typeof(StackPanel) && sChildName == ((StackPanel)(child)).Name))
{
break;
}
else if (child != null)
{
child = GetAttachmentStripPanelChild(child, sChildName);
if (child != null && (child.GetType() == typeof(StackPanel) && sChildName == ((StackPanel)(child)).Name))
{
break;
}
}
}
}
lee_sl
Contributor
4222 Points
864 Posts
Re: how to get the control (hosted inside the DataTemplate) out from the ItemsControl
Sep 24, 2008 04:57 PM | LINK
I have posted a sample on one way of doing this here
http://leeontech.wordpress.com/2008/09/24/getting-the-control-inside-a-datatemplate/
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/