Prevent drop-down menu to close - wpf

I have a big problem. I want to prevent an opened drop-down menu to close when hovering another menu item. How can I achieve this? Thanks.
Regards
Marius

You should have a look at the event that's called when you are hovering another item, probably LostFocusor LostMouseCapture, look at the function called aith this event (OnLostFocus or OnLostMouseCapture) and override this method like this :
public override void OnLostFocus()
{
base.OnLostFocus();
Popup popup = Template.FindName("PART_Popup", this) as Popup;
popup.IsOpen = true;
}
This way, the event won't close the drop down menu. (The example above is for a combobox)
I'm not sure about the right event to override. It depends on your problem. You can find the list of event here if these are not the right ones. But I think that's the way you should do it.

Related

Click outside component to unfocus

By default, I need to focus on another component (a button or a textBox) to unfocus from the current one.
I want to just click outside.
So for example if I click on a textBox and write something, than click outside the TextBox, I shouldn't be able to type because the component is unfocused.
(I hope my explanation is clear, if not, please say so in the comments)
You can achieve this functionality by attaching to PreviewMouseDown event of a Window. Also in order to Lose Focus you need another focusable control (e.g. a text box named _dummyControl) to move focus to.
public MainWindow()
{
this.PreviewMouseDown += PreviewMouseDownEventHandler;
}
private void PreviewMouseDownEventHandler(object sender, MouseButtonEventArgs e)
{
//check conditions here...
_dummyControl.Focus();
}
more info:
PreviewMouseDown event occurs just before a MouseDown or click event occurs. By attaching to this event your code can tell when user clicks on the window and at that time you should move focus to a hidden control to simulate unfocus functionality.

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

Listbox MousedoubleClick in WPF

I have code written on my Listbox mouse double click. I have a submit button too in my form . On mouse double click i want a mouse double click event to be fired first and then the button click
Is it possible?
Please reply
Thanks
Sharath
Easiest (and best practise) is simply to put the code for what to do on Button click into a seperate method (say OnOkClicked), then call this method from the Button Click event handler, as well as at the end of the ListBox DoubleClick event handler.
I nice pattern to implement is to implement an ICommand (like Josh Smith's RelayCommand - google it) on your modelview class, bind the button's Command property to it, and then in the list box's DoubleClick handler, invoke the Executed method of the command. This strucutre simplifies maintaining the logic of:
If nothing is selected in the list box, disable the OK button (Command.CanExecute would return false)
If something is selected in the list box, enable the OK button
If the list box is double clicked, select an item and invoke the OK command.
Hope this helps.
you can make your own control and implement ICommandSource which will give you the same properties

Systray context menu - why are my commands not enabled?

I'm creating a WPF app and have a system tray icon with a context menu. For the menu items I want to use WPF commands but when I assign them they are always greyed out even though the (same) commands are enabled in other places.
MenuItem menuItem = new MenuItem();
menuItem.Header = "Exit";
menuItem.Command = CustomCommands.ExitApplication;
Systray.AddMenuItem(menuItem);
It works fine when I assign click events and I have tried to create a CanExecute method for the command which always sets CanExecute to true, but that doesn't help either. Anyone got an idea why the menu items are disabled?
Update: As suggested, I added a command binding to the context menu. This had the effect that it works but only after you have clicked on the menu, i.e., at first the menu items are greyed out but once you click somewhere on the menu the options become enabled.
To solve this problem I called the following, after I added the menu items to the context menu:
CommandManager.InvalidateRequerySuggested();
Of the top of my head I'd guess you have to add a CommandBinding to the Menu or systray so that your command gets handled. Although I think if that were the case it would be enabled by default.
Yea I've seen this occur. Sometimes you have to tell the WPF CommandManager system to rerun the CanExecute methods. Try calling this once the ContextMenu is loaded: CommandManager.InvalidateQuerySuggested();
I had a similar issue. I feel my solution it's a bit of a hack, but I could really not get around this problem. I'm using a custom DelegateCommand implementation, and ebabling/disabling buttons and menu items works except for items in context menus. So, what I did was to handle the ContextMenuOpening event, then store the Items in a temp variable, call the Clear method in the ContextMenu and re-add the items right after. Works like a charm, but like I said, feels "hacky". It goes something like this:
private void ContextMenu_ContextMenuOpening (object sender, System.ComponentModel.CancelEventArgs e)
{
// HACK: For some reason items need to be removed and added back so that the command enablement requery works.
var menu = sender as ContextMenu;
if (menu == null) return;
var menuItems = menu.Items.ToArray();
menu.Items.Clear();
foreach (var menuItem in menuItems)
menu.Items.Add(menuItem);
}

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