Page 1 of 1

FrameworkElement.Parent property null when used in a template

Posted: 11 Jul 2017, 01:16
by pbastia
I'm having an issue where I have a custom control registering to events on the parent element, and sending it on a message bus type architecture.
Everything works fine, except when I am using this custom control inside a template: In this case, the property 'Parent' in the codebehind is null and never set.
<Expander ExpandDirection="Up" Grid.Column="1" Header="THINGS" VerticalAlignment="Bottom">
		<ItemsControl ItemsSource="{Binding BottomMenuBarViewModels}">
			<ItemsControl.ItemTemplate>
				<DataTemplate DataType="{x:Type vm:BottomMenuBarViewModel}">
					<Border>
						<Button>
							<views:MessagingView  Event="Click">
								Send message!
							</views:MessagingView >
						</Button>
					</Border>
				</DataTemplate>
			</ItemsControl.ItemTemplate>
		</ItemsControl>
</Expander>
Am I doing something wrong? Where could the issue be?
Thanks a lot,

Pierre

Re: FrameworkElement.Parent property null when used in a template

Posted: 14 Jul 2017, 02:27
by sfernandez
Have you tried to check the Parent on the Loaded event of your MessageView code-behind?
This event is called after element gets layout, so it should be connected to the tree and Parent property correctly set.