Hopefully this is a quick one for you WPF experts out here. Is there any easy XAML solution to allow my popup to move when the widow moves or changes size? The code is below. If not, I can always handle an event in the code behind.
<Grid>
<Canvas>
<Expander Header="details" HorizontalAlignment="Center" VerticalAlignment="Top" ExpandDirection="Down"
Expanded="Expander_Expanded" Panel.ZIndex="99" Collapsed="Expander_Collapsed" Name="expander">
<Popup PopupAnimation="Slide" Name="popup" Width="200" Height="200" StaysOpen="True" AllowsTransparency="True"
IsOpen="False" >
<Grid Background="Cornsilk">
<Grid.BitmapEffect>
<DropShadowBitmapEffect/>
</Grid.BitmapEffect>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock TextWrapping="Wrap" FontWeight="Bold">
Some example text
</TextBlock>
</Grid>
</Popup>
</Expander>
</Canvas>
</Grid>
This is pretty much Rick's answer but in code-behind, because I see absolutely no reason to introduce another dependency.
protected override void OnLocationChanged(EventArgs e)
{
popup.HorizontalOffset += 1;
popup.HorizontalOffset -= 1;
base.OnLocationChanged(e);
}
Just copy & paste to your window's code-behind and replace the popup
identifier with the one of your popup instance's identifier.
I find it much better, it's more succinct, it doesn't use events which you'll have to either unsubscribe from or memory leak.
And for your application's runtime, overriding will have better performances (nothing noticeable but micro-performance irrationalists, such as yours truly, would still just love it).
Here's a solution using:
This markup-only sample consists of a text box and a popup. The popup opens when the text box is focused. The popup follows the window as it moves by hooking the window's location changed event and forcing the popup to reposition itself accordingly.
<Grid>
<StackPanel>
<TextBox x:Name="textBox1" Width="200" Height="20"/>
<Popup PlacementTarget="{Binding ElementName=textBox1}" IsOpen="{Binding IsKeyboardFocused, ElementName=textBox1, Mode=OneWay}">
<TextBlock Background="White" Text="Sample Popup content."/>
<p:Attached.Operations>
<p:EventHandler Path="Loaded">
<p:ScriptHandler Path="@FindAncestor([Window], @AssociatedObject.PlacementTarget).LocationChanged">
@AssociatedObject.HorizontalOffset += 1;
@AssociatedObject.HorizontalOffset -= 1;
</p:ScriptHandler>
</p:EventHandler>
</p:Attached.Operations>
</Popup>
</StackPanel>
</Grid>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With