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
Related
I'm working with WPF.
My visual tree hierarchy as the following:
RadDiagram > RadDiagramShape > MyControl
The content of RadDiagramShape is mycontrol.
In myControl class, I have handled (MouseLeftButtonDown) Event, I put drag-drop code inside it(which I need it in another place). So, it's Direct Event not Tunneling or Bubbling!
While I'm moving my Custom Control which is the content of the RadDiagramShape, in RadDiagram, it doesn't move (It's trying to be dragged) because the MouseLeftButtonDown has been handled inside MyControl.
It prevents the event from bubbling up the Visual tree, preventing any parent handlers from being notified of the event.
I tried to handled the Event for the RadDiagramShape and for the RadDiagram as e.Handled = true;
but it did nothing because It's MouseLeftButtonDown and it's handled inside the root element so, it won't bubble or tunnel and I didn't override movement code, which I don't want to override it. Because I tried it before and it didn't give me the same slightly move that built-in in WPF.
How can I block MouseLeftButtonDown event in root element and let the event fired in the container(parent) level?
Please check in your control handler if 'OriginalSource' is the same control that you want or not.
if ((e.OriginalSource is TextBox) && (e.OriginalSource as TextBox).Name == "TextBoxName")
{
//Do every thing you want
}
Thank you leila karimi. You gave me the orientation, the condition itself didn't work. But I put another condition and it worked
void MyLabel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MyLabel dc = (MyLabel)sender;
if (dc.Parent.GetType() != typeof(RadDiagramShape))
{///....drop it}
}
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;
}
I'm building a custom control in WPF. I want to catch a mouse down event when my control is in focus but the user clicks outside the control. Is there a way to do that and if so how?
My control inherits from ListBox.
You can use UIElement.CaptureMouse and it's partner UIElement.ReleaseMouseCapture to capture all mouse events to a single control, regardless of what the mouse was over when the event occurred.
In your example I would capture the mouse when the control has focus, and release the mouse when the control looses focus.
Suppose you have a Window with a TextBox on it.
By registering to Window's MouseDown event,
MouseDown += new MouseButtonEventHandler(Window_MouseDown);
You can use following code
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (TextBox1.IsFocused)
{
MessageBox.Show("TextBox1 in focus.");
}
}
To catch this event if TextBox1 is in focus.
I'm starting to make some tests with a touch screen and I've found that if a UIControl has the "IsManipulationEnabled" attribute set to true then the MouseRightClick Events fired by the press and hold gesture (WIN7) is not captured. Am I doing something wrong?
public MainWindow()
{
InitializeComponent();
WC_Rectangle.IsManipulationEnabled = true;
WC_Rectangle.MouseRightButtonUp += new MouseButtonEventHandler(WC_Rectangle_MouseRightButtonUp);
}
void WC_Rectangle_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
System.Diagnostics.Debug.WriteLine("RIGHT CLICK : " + sender.ToString());
}
After setting IsManipulationEnabled = true; all touchevents are captured and handled by the WC_Rectangle which does transform them to Manipulation events. So the touchevents do not tunnel back to the control that raised them which in turn means the control can't promote unhandled touch events to mouse events (the default). see:
http://nui.joshland.org/2010/04/why-wont-wpf-controls-work-with-touch.html
If you cancel the manipulation events you should get the mouse events.
Use Behavior Instead
I wanted to get the type of the control on mouseover. Please help
You can get the type of the UIElement over which the mouse is currently moving using the MouseMove event. Since this is a bubbling event you can attach a handler to the container such as a Canvas.
The UIElement over which the mouse is currently moving can be aquired from the the MouseEventArgs OriginalSource property.
Hence to determine the type over which the mouse is moving you could use code like this:-
void Canvas_MouseMove(object sender, MouseEventArgs e)
{
Type currentType = e.OriginalSource.GetType();
// Make decisions based on value of currentType here
}
However you need be careful, MouseMove fires frequently as the user moves the mouse so you might want to defer any heavy work until there is some time period after the last mouse move.
There is unfortunately no bubbling mouse over event.
The other alternative is to attach the same MouseEnter handler to each child UIElement you add to the Canvas. You could use sender instead of e.OriginalSource in that case. You would have to be careful to remove the handler if the element is removed from the Canvas, else you can create what would appear to be a memory leak.
Add mouse_enter event to the control.
You can get the type with a line of code as follow
var x = sender.GetType();
You can then compare it using something like:
if (x.Equals(typeof(TreeView)))