Close popup when mouse leaves - wpf

I have a popup window which is opened, and then within that popup the user can drag an item (from within the popup) which starts a DragDrop.DoDragDrop();
How can I have the popup automatically close when the users cursor leaves the Popup window?
I've tried a few options such as MouseEnter/Leave events and IsMouseOver properties, but none of these work as I believe the DragDrop initiates mouse capture.

This will work:
popup.Child.MouseLeave += (u, l) => popup.IsOpen = false;

Related

WPF How to prevent interaction with controls in background when Popup is open

When I display a Popup all other controls in the background appears to be not active (i.e. when I hover over a button for example it does not change a style). When I click somewhere to dismiss the Popup and it just happens to be a button where I clicked, that button takes a hit. This is very confusing, because one would assume that they can click anywhere to dismiss the popup and not just the empty space.
How to prevent interaction with controls in background when Popup is open?
OR
How to make controls in background react to the mouse hover even when Popup is open (like ToolTip)?
Setting IsHitTestVisible either to true or false seems to have no effect.

WPF setting focus back to textbox on modal Window close

Assume there is a WPF window with multiple textboxes and one button. Currently the cursor is blinking in one of the textBox and then on button click I launched a modal window and then on close of this Window I want the Cursor blinking back to the textbox where it was before launch of the modal window.
How I can achieve it in WPF?
FocusManager.GetFocusedElement
It will give the button, not the textbox.

How to add already existing ToolStrip Buttons to a ToolStrip Dynamically

I have a Winform Applicaion useing an MDI Form. On the MDI Form I have a ToolStrip with buttons (Buttons have images) on it acting as the main menu buttons for the application. So when user clicks on a button on the toolstrip the mdichild form for that button opens the child form.
So I have six buttons with images already created and in the project. But I want the user to select the buttons they want to appear on the toolstrip.
So the user opens the application and there is only one button on the toolstrip. The user clicks on that button and a child screen opens showing all the available existing buttons that could be on the toolstrip. The user selects the buttons they want to appear on the toolstrip then clicks that save button on the child screen.
What I want is that as soon as the user clicks that save button the buttons that the user selected should automatically appear on the toolstrip.
Right now I have to have the user close the applicaiton then reopen it for the buttons they selected to appear on the toolstrip.
How so I get the buttons to appear automatically?
Just create all of the ToolStripButtons, and set each one's Visible property to false. When the user picks them to be shown, change the Visible property of the ToolStripButton to true. They'll automatically appear on the ToolStrip.
I tested using VS2010 with Oxygene from RemObjects (formerly AKA Delphi Prism).
Start a new WinForms application
Drop a ToolStrip on the window. Right-click it and choose Insert standard items.
Double-click the New button (newToolStripButton, the one on the left end), and add the following code to the newToolStripButton_Click handler:
// Oxygene version: helpToolStripButton.Visible := not helpToolStripButton.Visible;
helpToolStripButton.Visible != helpToolStripButton.Visible;
Run the application, and click the newTooStripButton repeatedly, and watch the right-most ToolStripButton (the Help button) appear and disappear from the ToolStrip.
You can create any ToolStrip and add it to a MenuStrip.DropDownItems.Add.
The click EventHandler must be a (s,e) function.
ToolStripMenuItem ts = new ToolStripMenuItem();
ts.Name = $"MyMenuStrip";
ts.Text = "New MenuStrip";
ts.Click += new EventHandler(this.ToolStripMenuItem_Click);
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem clickedMenuItem = sender as ToolStripMenuItem;
Trace.WriteLine($"Clicked: {clickedMenuItem.Text}");
}

Popup window and context menu

I am using the ToolStripDropDown to host the user control as the pop-up window. The problem is when a context menu strip is displayed from within this pop-up window, the pop-up itself closes in the moment the context menu opens.
I have tried to subclass the ContextMenuStrip and added WS_EX_NOACTIVATE to CreateParams but nothing changed. First I thought that there is no way to do this since it is common behavior but then I tried to put a TextBox class onto the pop-up user control and invoke the Edit control context menu - and the parent pop-up window did not close.
What am I missing?
Had a similary Problem. On my UserControll was a toolstrip. When I pressed the toolsstripdropdownbutton the dropdown was shown but the popup disapeared.
The reason was that popup.Autoclose was true. After Setting to false the Popup is not closed any more.
ToolStripDropDown popup = new ToolStripDropDown();
popup.AutoClose = false; //Set to FALSE
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(userControl1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(button1, new Point(100,100));
Actual Solution should be the one in Martin's final comment:
Use ContextMenu Instead of ContextMenuStrip
That one worked for me, and the ToolStripDropDown no longer closes by itself when right clicking one of its content controls, like it should. We still need it to AutoClose, disabling AutoClose on ToolStripDropDown will do bad things, it is supposed to close on losing focus. Example: open any other app window, and the ToolStripDropDown will continue to appear on top

How do I disable the exit button on a Silverlight 3 Child Window?

I mean the small exit/cancel button marked with an X in the top right hand corner. I want to implement a Logon dialog box that accepts a username/password so obviously I don't want the user to be able to dismiss the modal pop up. If it is not possible to remove or disable the button then is there some way I can intercept the closing event and stop it closing?
You can use the HasCloseButton property of the ChildWindow to hide the close button.
Please let me know if this helps.
Ezequiel Jadib
The code below prevents a ChildWindow from ever closing, effectively disabling the X button. Modify to suit your business logic.
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
e.Cancel = true;
}
Select child window and Press F4. It will show the property window. Then goto HasCloseButton property and uncheck the checkbox.
Enjoy
HasCloseButton="False" ..
This property used to hide the 'X' button In ChildWindow

Resources