Here an idea/way how you can solve your request to find the parent control.
Capture the parent control in
OnVisualParentChanged
:
public partial class ChildUserControl : UserControl
{
private ParentUserControl parentUserControl = null;
protected override void OnVisualParentChanged(DependencyObject oldParent)
{
base.OnVisualParentChanged(oldParent);
parentUserControl = this.Parent as ParentUserControl;
}
void DoSomethingWithParent()
{
if (parentUserControl != null)
{
parentUserControl.ParentMethod();
}
}
}
Parent Control XAML, note: child should not be in e.g. a grid for the code above.
<usercontrol>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication4" x:Class="MyWPFControls.ParentUserControl"
mc:Ignorable="d" Height="159.2" Width="209.6" Background="#FFD8BBBB">
<local:childusercontrol horizontalalignment="Left" height="32" margin="147,117,0,0" verticalalignment="Top" width="53" xmlns:local="#unknown">
Background="#FFB28484"/>
</local:childusercontrol></usercontrol>
I hope it helps.