I have a custom control which has it's template defined and the template contains below code:
<FlipView Grid.Row="3"
Grid.ColumnSpan="2" x:Name="FlipView1" BorderBrush="Black"
ItemsSource="{Binding ItemsCollection, RelativeSource={RelativeSource TemplatedParent}}">
<FlipView.ItemTemplate>
<DataTemplate>
<ScrollViewer>
<Grid>
<local:UserControlA x:Name="PART_UserControlA"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<local:UserControlB Grid.Column="1"
View="{Binding View}"
x:Name="PART_UserControlB"
ItemsSource="{Binding ItemsSourcePropertyOfAnItemInItemsCollection}"
ItemTemplate="{Binding TemplatePropertyOfAnItemInItemsCollection}" />
</Grid>
</Grid>
</ScrollViewer>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
In code behind of my custom control, I have this code to load the controls in the template (I had to do this trick since GetTemplateChild returns null because PART_UserControlB is again a part of the template of FlipView and GetTemplateChild does not recursively gets the templated child):
protected override void OnApplyTemplate()
{
FlipView flipView = GetTemplateChild("FlipView1") as FlipView;
DataTemplate dt = flipView.ItemTemplate;
DependencyObject dio1 = dt.LoadContent();
DependencyObject dio = (dio1 as ScrollViewer).Content as DependencyObject;
foreach (var item in FindVisualChildren<UserControlB>(dio))
{
if (item.Name == "PART_UserControlB")
{
UserControlB controlB = item;
controlB.ApplyTemplate();
controlB.PointerPressed += OnPointerPressed;
}
}
}
public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
Problem is that when I tap on an item in UserControlB, it does not trigger the OnPointerPressed event for that control. It is like I am not getting the same instance of the UserControlB in the code behind.