当WinForms窗口的位置发生变化时,移动WPF弹出位置

时间:2013-10-24 10:07:27

标签: c# wpf winforms custom-controls

我在WPF中制作了一个自定义控件,ControlTemplate包含Popup控件:

<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}">

自定义控件通过以下代码托管在WinForms应用程序中:

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

我希望在窗口位置发生变化时自动重新定位弹出窗口。我看到了几个答案here建议获取窗口引用并注册到他的LocationChanged事件,但它不适合我,因为它托管在winForms窗口中。

任何建议都会有所帮助:)

1 个答案:

答案 0 :(得分:2)

您是否只能通过LocationChanged

连接ElementHost事件

TitleBar课程中:

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

在托管代码中:

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