Reputation: 13
I have a modeless dialog, which is owned by the main window and snapped to the certain reference point in the main window. I do this by computing the top-left corner of the reference point in the screen co-ordinates, and assigning it to the Top and Left properties of the owned window.
When I open that owned modeless dialog, everything is OK. Then, when I move the main window by the mouse, I'd like the owned dialog to move at the same time. To achieve this, I re-compute the screen position of the reference point and pass it to the Top and Left properties of the owned window in the MouseMove event handler. Apparently, such thing should work (cf. the thread: Lock a window position to another window? ). In my case, however, the owned window doesn't move with the owner, rather, only when I drop the main window and hover on its client area the owned window jumps to its intended position.
In MFC, I could work around by sending the WM_WINDOWPOSCHANGED event to the owned window. But what can I do in WPF?
Upvotes: 0
Views: 1685
Reputation: 10797
Code below is working very well for me. There is also a bit more complex binding solution.
In your main window get the event for LocationChanged:
<Window ... LocationChanged="Window_LocationChanged">
In the code-behind, keep the dialog as a field of the MainWindow. Then have the following code:
public partial class MainWindow : Window
{
// keep a reference to the dialog
Dialog m_dlg;
public MainWindow( ) {
InitializeComponent( );
}
...
private void createDialog( ) {
m_dlg = new Dialog( );
realign( );
m_dlg.Show( );
}
// change the dialog left and right when the main window moves:
private void Window_LocationChanged( object sender, EventArgs e ) {
realign( );
}
private void realign( ) {
if( m_dlg != null ) {
m_dlg.Left = this.Left + 30;
m_dlg.Top = this.Top + 30;
}
}
...
}
Upvotes: 2