Search code examples
c#wpfxamluser-controls

Reference a control from parent window in a custom user control


I am trying to create a re-useable popup user control. It needs to be a usercontrol because it needs to contain a button and a hyperlink which will need code behind when clicked. I want to set the PlacementTarget of the popup to a control (e.g. button) in the parent window, and would like to be able to pass in the control name so that the popup will open next to the relevant control. I have tried the following, but it is not working.

User Control:

<UserControl
  x:Class="SampleTestProject.WPF.VrtContactUserTooltip"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Name="parent">
  <Grid>
    <Popup
    Name="ContactTooltip"
    PlacementTarget="{Binding Path=Target, ElementName=parent}"
    DataContext="{Binding Path=Contact}"/>
    </Grid>
</UserControl>

Code behind for user control:

 public partial class VrtContactUserTooltip : UserControl
  {
public VrtContactUserTooltip()
{
  InitializeComponent();
}

#region Properties

#region Target

/// <summary>
/// Gets or sets the Target of the tooltip
/// </summary>
public string Target
{
  get { return (string)GetValue(TargetProperty); }
  set { SetValue(TargetProperty, value); }
}

/// <summary>
/// Identified the Target dependency property
/// </summary>
public static readonly DependencyProperty TargetProperty =
  DependencyProperty.Register("Target", typeof(string),
    typeof(VrtContactUserTooltip), new PropertyMetadata(""));

#endregion

#region Contact

/// <summary>
/// Gets or sets the Contact of the tooltip
/// </summary>
public Contact Contact
{
  get { return (Contact)GetValue(ContactProperty); }
  set { SetValue(ContactProperty, value); }
}

/// <summary>
/// Identified the Contact dependency property
/// </summary>
public static readonly DependencyProperty ContactProperty =
  DependencyProperty.Register("Contact", typeof(Contact),
    typeof(VrtContactUserTooltip), new PropertyMetadata(null));

#endregion
#endregion
  }
}

Where user control is being used:

 <Button
    x:Name="PopupButton3" />
  <wpf:VrtContactUserTooltip
    Target="{Binding Source={x:Reference PopupButton3}}"
    Contact="{Binding Path=Contact}"/>

Is it possible to do this (i.e. pass a control name from the parent control into the user control and bind to it)?


Solution

  • I ended up setting the PlacementTarget in the constructor of the window that the user control was being used in. I put the following line in the constructor of the parent window:

    ContactPopup3.ContactTooltip.PlacementTarget = PopupButton3;
    

    This is not ideal (as it will need to be set in the code behind every time the user control is used), but it solved the problem.