Omri Btian
Omri Btian

Reputation: 6547

Move WPF popup location when WinForms window's location changes

I have a custom control made in WPF, with a ControlTemplate containing a Popup control:

<Popup x:Name="PART_Popup" 
       PopupAnimation="Fade"
       Width="{TemplateBinding Width}"
       AllowsTransparency="True"
       IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsPopupOpen, Mode=OneWay}" 
       Placement="Bottom"  
       PlacementTarget="{Binding ElementName=PART_Border}">

the custom control is hosted in a WinForms app by the following code:

var wpfHost = new ElementHost();
wpfHost.Dock = DockStyle.Fill;
wpfHost.Child = new TitleBar();
Controls.Add(wpfHost);

I want to make the popup reposition itself when the window location changes. I saw a couple of answers here suggesting to get the window reference and register to his LocationChanged event but it doesn't work for me since it is hosted in a winForms window.

Any suggestions would be helpful :)

Upvotes: 1

Views: 762

Answers (1)

Sheridan
Sheridan

Reputation: 69959

Can you not just connect to the LocationChanged event through the ElementHost?:

In TitleBar class:

public void WinFormsParent_LocationChanged(object sender, EventArgs e)
{
    // Do what you want here
}

In hosting code:

var wpfHost = new ElementHost();
wpfHost.Dock = DockStyle.Fill;
TitleBar titleBar = new TitleBar();
LocationChanged += titleBar.WinFormsParent_LocationChanged;
wpfHost.Child = titleBar;
Controls.Add(wpfHost);

Upvotes: 2

Related Questions