Trigger StackPanel Mouse Event programmatically - wpf

I have the stackpanel with PreviewMouseUp event. Actually i need to trigger(call stackPanelMouseUp()) the mouseup event by programmatically ( That means without performing the mouse down operation on StackPanel)
stackPnl.PreviewMouseUp += stackPanelMouseUp;
private void stackPanelMouseUp(object sender, MouseButtonEventArgs e)
{
}
Thanks,
Sowndaiyan

Related

Why does the KeyDown event bubble up from a TextBox?

If I type a letter into a TextBox, and its content changes according to my keypress, why does the KeyDown event continue bubbling up? I would have thought this would be 'handled' at this stage.
Since KeyDown event is a bubbling event, that's why its bubbled to its parent in your case Window. If you don't want that to bubbled to your window, you need to mark it as handled in your textBox handler itself like this -
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
Whereas, if you try to hook the event PreviewKeyDown in your textBox, you will see that - Window's PreviewKeyDownEvent gets called first and later that of your textBox. Reason behind that is, it's a tunelling event. For routing strategies, refer to this link - Routing Strategies
EDIT
Morevoer, if you want to check if the KeyDown event comes from textBox, you can check the OriginalSource of your eventArgs -
private void Window_KeyDown(object sender, KeyEventArgs e)
{
// Check to make sure event comes from window and not from textbox.
if(e.OriginalSource is Window)
{
}
}

C# how to create an event handler for listBox1.MouseDown?

I have been trying to make it so that when i right click a listbox item it selects it, but I can't figure out how to declare an event handler for my listbox for mouse down.
Any Ideas?
in c#:
listBox1.MouseDown += delegate(object sender, MouseEventArgs e){ ... };

Why does implementing WPF DragDrop behaviour stop ListBoxItems becoming selected on the first click?

I have been implementing basic drag and drop functionality using WPF and C# quite successfully for some time now. I have always had one problem after implementing it though... for some reason, the drag and drop functionality stops the ListBoxItems from becoming selected (on the first click).
If I click on a ListBoxItem but don't drag it, it doesn't get selected and the drag icon appears momentarily. On the next click I can then select any of the ListBoxItems and the drag icon does not appear. This cycle then repeats... first click won't select, second one will.
Below is a typical implementation of my drag and drop code, taken from the Micorsoft MCTS 70-511 'Training kit' book.
private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
object data = (ListBoxItem)(FrameworkElement)sender;
if (data != null) DragDrop.DoDragDrop(ListBox, data, DragDropEffects.Copy);
e.Handled = false;
}
private void ListBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(ListBoxItem))) e.Effects = DragDropEffects.Copy;
}
private void ListBox_Drop(object sender, DragEventArgs e)
{
object data = e.Data.GetData(typeof(ListBoxItem));
if (data != null) DoSomethingWith((DataType)((ListBoxItem)data).DataContext);
}
The drag and drop works fine, but the item selection doesn't... I presumed that by adding e.Handled = false in the ListBox_PreviewMouseLeftButtonDown handler, the ListBoxItem selection mechanism could handle the click event, but it never reaches that far.
I also tried handling the drag initiation in the MouseLeftButtonDown handler instaed of the PreviewMouseLeftButtonDown handler, but the ListBoxItem selection mechanism handles the click event and it never reached that drag drop handler.
There must be a way to initiate the drag drop operation and still have the ListBoxItem that was clicked on become selected, but I still haven't managed to find it... any clues anyone?
UPDATE >>>
Thanks to the MSDN article #icebat provided a link for, I've managed to get the drag and drop functionality working perfectly. It is now as follows:
private void SourceListBox_MouseMove(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
object data = ((ListBox)(FrameworkElement)sender).SelectedItem;
if (data != null)
DragDrop.DoDragDrop(SourceListBox, data, DragDropEffects.Copy);
}
}
private void TargetListBox_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DragObject))) e.Effects = DragDropEffects.Copy;
}
private void TargetListBox_Drop(object sender, DragEventArgs e)
{
object data = e.Data.GetData(typeof(DragObject));
if (data != null) DoSomethingWith((DragObject)data);
}
Just use MouseMove event instead of MouseDown for drag and drop. You can find more information and some code in the Drag and Drop article on MSDN.

WPF Mousedown => No MouseLeave Event

I'm building a Windows Presentation Foundation control with Microsoft Blend.
When I leave my control by pressing the left-mouse-button, the MouseLeave-Event is not raised. Why not?
This is intended behaviour: When you are doing mousedown on a control and leaving the control, the control STILL retains its "capture" on the mouse, meaning the control won't fire the MouseLeave-Event. The Mouse-Leave Event instead will be fired, once the Mousebutton is released outside of the control.
To avoid this, you can simple tell your control NOT to capture the mouse at all:
private void ControlMouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)
{
Control control = (Control) sender;
control.Capture = false; //release capture.
}
Now the MouseLeave Event will be fired even when moving out while a button is pressed.
If you need the Capture INSIDE the Control, you need to put in more effort:
Start tracking the mouseposition manually, when the mousekey is pressed
Compare the position with the Top, Left and Size Attributes of the control in question.
Decide whether you need to stop the control capturing your mouse or not.
public partial class Form1 : Form
{
private Point point;
private Boolean myCapture = false;
public Form1()
{
InitializeComponent();
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
myCapture = true;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (myCapture)
{
point = Cursor.Position;
if (!(point.X > button1.Left && point.X < button1.Left + button1.Size.Width && point.Y > button1.Top && point.Y < button1.Top + button1.Size.Height))
{
button1.Capture = false; //this will release the capture and trigger the MouseLeave event immediately.
myCapture = false;
}
}
}
private void button1_MouseLeave(object sender, EventArgs e)
{
MessageBox.Show("Mouse leaving");
}
}
of course you need to stop the own tracking ( myCapture=false;) on MouseUp. Forgot that one :)
When I don't get mouse events I expect I typically use Snoop to help me understand what is happening.
Here are a couple of links:
1- Snoop (a WPF utility)
2- CodePlex project for Snoop
And for completeness and historical reasons (not the bounty - it doesn't make sense having two duplicate questions - you should probably move it into one if not too late)...
I made a thorough solution using global mouse hook here (approach 2)
WPF: mouse leave event doesn't trigger with mouse down
And simplified its use - you can use it by binding to commands in your view-model - e.g.
my:Hooks.EnterCommand="{Binding EnterCommand}"
my:Hooks.LeaveCommand="{Binding LeaveCommand}"
my:Hooks.MouseMoveCommand="{Binding MoveCommand}"
...more details in there
Old question but I came across the same problem with a Button (MouseLeave does not fire while MouseDown because MouseDown Captures the Mouse...)
This is how I solved it anyway:
element.GotMouseCapture += element_MouseCaptured;
static void element_MouseCaptured(object sender, MouseEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
element.ReleaseMouseCapture();
}
Hope that helps someone looking for a quick fix :P

Loss of Silverlight mouse up events after mouse capture?

I created a very simple test control that has a Rectangle on a canvas (within other containers, but inconsequential). The Rectangle has event handlers for mouse down, mouse move, and mouse up. If I capture the mouse in the Rectangle's MouseLeftButtonDown event, I do not receive a corresponding MouseLeftButtonUp event.
Some code:
private void rect1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_captured = CaptureMouse())
{
_offset = new Point(Canvas.GetLeft(rect1), Canvas.GetTop(rect1));
_origin = e.GetPosition(RootCanvas);
e.Handled = true;
}
}
private void rect1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_captured)
{
ReleaseMouseCapture();
_captured = false;
e.Handled = true;
}
}
I attached event handlers for the container elements as well, just to make sure one of them wasn't getting the mouse-up event somehow, but none of them were. Is there an expectation of this in Silverlight that I haven't yet learned?
I think you are little confused about what is actually capturing the mouse events.
Consider when you do this:-
if (_captured = CaptureMouse())
what object is the CaptureMouse actually being called against?
Answer: The user control for which your code is the code-behind. Had you wanted the rectangle to capture the mouse you would do:-
if (_captured = rect1.CaptureMouse())
CaptureMouse(); from mouseDown Event and then try.

Resources