How to cancel a Context Menu Opened Event? - wpf

I want to cancel the Context Menu Opened Event of a TreeViewItem
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
e.Handled = true;
}
i thought this should work but instead i got a tiny context menu that has no menu items but i would like to have nothing appearing.
Thanks

You should use the ContextMenuOpening event of the control that owns the context menu, which is fired right before the context menu opens. There you can change the context menu or suppress it. Your code for the event to suppress works if you use the Opening event, see this article for further infos.

Related

Show ContextMenu for TreeViewItem by raising events in code

I want to write a coded UI test for my treeview with context menu. The idea is to validate if the context menu is added to treeViewItem.
So far I tried to raise PreviewMouseRightButtonUp event. This is not working.
If I subscribe to the event then I get a callback after right-clicking the tree item. But raising the event manual does not cause the context menu to be added.
Here is how I am raising the event:
MouseButtonEventArgs eventArgs = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Right);
eventArgs.RoutedEvent = Mouse.MouseUpEvent;
uiElement.RaiseEvent(eventArgs);
If you want to validate if the context menu is added to treeViewItem, why not right-click on the tree item and look for the existence of a menu in the window.

Avoid radcombobox suggest list from being collapsed

I have a RadDropDownList in suggestappend mode and a usercontrol as keyboard, this have buttons with ControlStyles.Selectable = false. the MouseUp event fires a SendKeys.Send(key).
The thing is when I focus in the RadDropDownList and write with my keyboard (UserControl) the suggest list appears for a milisecond and desappear.
I tried to control the popup event but it seems to have nothing to do with the suggest list.
how can i keep it opened showing suggestions until user leaves the RadDropDownList?
Here is how to access the auto complete suggest popup and cancel the closure of the popup:
radDropDownList1.DropDownListElement.AutoCompleteSuggest.DropDownList.PopupClosing += DropDownList_PopupClosing;
. . .
void DropDownList_PopupClosing(object sender, Telerik.WinControls.UI.RadPopupClosingEventArgs args)
{
args.Cancel = true;
}

XAML Window MouseDown event beats Image event

I'm using WPF to design a borderless, movable application window.
In order to manually perform the ability to drag and drop the window, I've added an OnMouseDown event to the <Window> element, that executes a corresponding C# function (this.DragMove()).
Additionally, I need an <Image> button to allow some operation (with the OnMouseUp event this time). Note that it has to be an Image tag, and not a Button.
Unfortunately, the Image event fired only when the right mouse button is clicked, probably because the left button is held to the window event. Am I right?
When someone clicks the Image button, I want only the Image event to be triggered. How can I do it?
Problem you're facing is most probably related to event routing. The idea is that if your handler doesn't mark event as a Handled it will be routed to the next listener unless reach end of chain or one of listeners/handlers will set it as Hadnled.
So if you have MouseDown event handler for both Window and Image you should keep in mind that routing will stop at a point when you will set e.Handled = true;:
private void Window_OnMouseDown(object sender, KeyEventArgs e)
{
e.Handled = false; // will NOT break event chain;
}
You can always check a type of sender so it will make possible for you to differ Image and Window events:
private void Image_OnMouseDown(object sender, KeyEventArgs e)
{
if (sender is Image)
{
// Handle Image.MouseDown
e.Handled = true; // we don't need to push event further;
}
}
Its because of WPF bubbling and tunnelling events. so what u can do is whenever u handle event on button use bubbling for that means you can use previewevents for that for both button and window and whenever you just want to handle event for button then after last line of code in button click just write down like this.
e.handled=true;
// here e is the event argument which u will get in your preview event.so now window dragging event will not work.
i would just suggest first clear the idea of bubbling(preview mouse event) and tunneling in wpf.
Difference between Bubbling and Tunneling events
and go through some of the example of bubbling and tunnelling. you will get better idea.
http://www.codeproject.com/Articles/464926/To-bubble-or-tunnel-basic-WPF-events

How do prevent a WPF text box inside a combo from reacting to right mouse clicks?

I creating a custonmized box class (inherits from ComboBox). I don't want the text box to react to right mouse clicks. I can get rid of the context menu by setting this to null in ApplyTemplate, but right mouse clicks move the cursor. I tried hooking up PreviewMouseRightButtonDown in ApplyTemplate and setting Handled to True, but the event still gets through which is strange as it seems to work for the left click.
The cursor actually moves when the mouse button is released, so you want mark the MouseRightButtonUp event as handled. You could override OnMouseRightButtonUp:
protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
{
base.OnMouseRightButtonUp(e);
e.Handled = true;
}
Or you could attach a class handler to the MouseRightButtonUp event to mark it as handled:
static MyComboBox()
{
EventManager.RegisterClassHandler(
typeof(MyComboBox),
MouseRightButtonUpEvent,
new MouseButtonEventHandler(MyComboBox_MouseRightButtonUp));
}
private static void MyComboBox_MouseRightButtonUp(
object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
That will also prevent the context menu from being created without you having to set it to null explicitly.

ToolStrip Buttons flashes

I have a windows form with a toolstip in it with several buttons.
When the mouse is over a button of the toolstip then the toolstrip button starts to flash... looks like it gets and loses focus every second.
That results for the click to do nothing if the user click at the time that the button has no focus therefore the user has to click the button again and again util he gets the timing correct.
Does anyone knows anything about this?
I rally need some answers as soon as possible...
Thank you very much
I have found the reason...
The toolstrips in windows forms have by default the tooltips set to Auto and if the tooltip opens on the taskbar then the toolstrip loses focus.
The solution to this is to either disable the tooltips or to set it to manual and show the tooltip at another place.
Here is the code for showing the tool tip above the item manually:
private readonly ToolTip currentToolTip = new ToolTip();
private void ToolStripItem_MouseEnter(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Show(item.ToolTipText, item.Owner, item.Bounds.X, -20);
}
private void ToolStripItem_MouseLeave(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Hide(item.Owner);
}
You have to add the event handlers to all your ToolStripItems and set the ToolStrips' ShowItemToolTips to false.

Resources