Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just to <strong>add on to NathanAW's great solution</strong> above, I thought I'd <strong>point out some context</strong>, such as <i><b> where </b></i> to place the C# code in this case. I'm still pretty new to WPF so I struggled at first to figure out where to put NathanAW's code. When I tried putting that code in the constructor for the UserControl that hosted my Popup, <code>Window.GetWindow()</code> always returned <code>Null</code> (so the "bump" code never executed). So I thought that other newbies might benefit from seeing things in context. </p> <p>Before showing the C# in context, here's some example XAML context to show some relevant elements and their names:</p> <pre><code>&lt;UserControl x:Class="MyNamespace.View1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" &gt; &lt;TextBlock x:Name="popupTarget" /&gt; &lt;Popup x:Name="myPopup" Placement="Bottom" PlacementTarget="{Binding ElementName=popupTarget}" &gt; (popup content here) &lt;/Popup&gt; &lt;/UserControl&gt; </code></pre> <p>Then in the code-behind, to avoid having <code>Window.GetWindow()</code> return <code>Null</code>, wire up a handler to the Loaded event to house NathanAW's code (see <a href="https://stackoverflow.com/a/304604/718325">Peter Walke's comment</a> on a similar stackoverflow discussion for example). Here's exactly how it all looked in my UserControl code-behind:</p> <pre><code>public partial class View1 : UserControl { // Constructor public View1() { InitializeComponent(); // Window.GetWindow() will return Null if you try to call it here! // Wire up the Loaded handler instead this.Loaded += new RoutedEventHandler(View1_Loaded); } /// Provides a way to "dock" the Popup control to the Window /// so that the popup "sticks" to the window while the window is dragged around. void View1_Loaded(object sender, RoutedEventArgs e) { Window w = Window.GetWindow(popupTarget); // w should not be Null now! if (null != w) { w.LocationChanged += delegate(object sender2, EventArgs args) { var offset = myPopup.HorizontalOffset; // "bump" the offset to cause the popup to reposition itself // on its own myPopup.HorizontalOffset = offset + 1; myPopup.HorizontalOffset = offset; }; // Also handle the window being resized (so the popup's position stays // relative to its target element if the target element moves upon // window resize) w.SizeChanged += delegate(object sender3, SizeChangedEventArgs e2) { var offset = myPopup.HorizontalOffset; myPopup.HorizontalOffset = offset + 1; myPopup.HorizontalOffset = offset; }; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload