Silverlight: Event Handler Issue - silverlight

So I have a Grid on a page that has a few UserControls on it. Each UserControl has a MouseLeftButtonDown event registered, as does the Grid. Before i added the event to the grid, the events on the user controls worked fine. But now that i have the event on the Grid, only the grid event fires regardless of where i click. None of the UseControls are capturing the event.
What do i need to do to allow the MouseLeftButtonDown events on the UserControls to fire while still having the MouseLeftButtonDown event on the Grid?

What you describe is very unusual.
What would often happen in this case it that both events fire. Since MouseLeftButtonDown is a bubbling event when you click on a UserControl it fires its MouseLeftButtonDown, if the handler attached to it doesn't set the Handled property of the MouseButtonEventArgs parameter to True then the event will bubble up to the parent and so on. If the parent controls also have code attached to their MouseLeftButtonDown events that code will also run.
Are sure that in fact the UserControl events don't fire or is that you happen to observe that the Grid event was always firing. If you are absolutely certain that attaching a event handler to the Grid actually prevents the UserControl events from firing can you edit your question with a small Repro, its very hard to see how this could be the case.

Related

Lostfocus actingas gotfocus for combobox in wpf

I'm using a combobox for which I have Lostfocus event set. But the lostfocus event is fired even when the combobox gets focus i.e gotfocus. Why is it happening so? If that is the default behavior is there any alternative solution for this?
From MSDN UIElement.LostFocus Event :
Because this event uses bubbling routing, the element that loses focus might be a child element instead of the element where the event handler is actually attached. Check the Source in the event data to determine the actual element that gained focus.
You can also use the IsFocused property of your ComboBox check if it has lost focus or not.

WPF TouchUp Event Strange behaivor

My WPF app functions perfectly, but only when using a mouse. Troubles start when using it on a device with a touch screen..
I have a grid that handles MouseLeftButtonUp and TouchUp events.
Now, I press on the grid, it handles related events, then I press on some other control, that other control catches TouchUp event as expected, then TouchUp event is transformed into MouseLeftButtonUp event, which is also something to expect.
However, the newly fired MouseLeftButtonUp event is fired NOT for the control that I pressed on, but for the above mentioned grid! Why does it behave this way?
Thank you in advance...
This is normal behaviour for all RoutedEvents. From the UIElement.MouseLeftButtonUp event page on MSDN:
Although this routed event seems to follow a bubbling route through an element tree, it actually is a direct routed event that is raised and reraised along the element tree by each UIElement.
MSDN provides far more answers and far quicker than Stack Overflow.

WPF: Prevent Combobox from Swallowing Keydown Event

So I have a window that handles the KeyDown event. Everything works as expected except under two conditions:
The up / down arrow keys are pressed and
A combo box on the window has more than one item.
Even if I've never clicked on the combo box it doesn't seem to matter. The SelectionChanged event on the combobox fires before the Window even fires its KeyDown event. This seems highly counter-intuitive to me.
I don't know enough about WPF event propagation to even know where to start looking for a solution. Any recommendations?
You should subscribe to PreviewKeyDown event instead.

differences between routed events and attached events and what differences they make?

I am just going through the WPF concepts I came across these routed events ,dependency properties and attached events.
I think I am not able to understand the concepts behind them and why they were being called in the place of .net originated methods?
Routed Events provide the ability for different controls in the element tree to react to events.
For instance, if we have a Window containing a StackPanel containing a Button and someone presses the mouse key on the button, the events will be fired in this order:
PreviewMouseDown on Window
PreviewMouseDown on StackPanel
PreviewMouseDown on Button
MouseDown on Button
MouseDown on StackPanel
MouseDown on Window
These "preview" events use a behavior called Tunneling. The normal events Bubble up again.
If you set the Handled property of the EventArgs to true, the tunneling and bubbling will stop. Tunneling or Bubbling is called the Routing Strategy.
This enables handling a variety of situations, for instance:
Preventing any child element of the StackPanel to receive mouse down events. (Set Handled to true on StackPanel.PreviewMouseDown).
Handling key presses on a Window that have not been handled by any other control (Window.KeyDown)
I would advise not looking into Attached Events untill this becomes absolutely necessary. The scenario's for attached events are very limited. Attached events are also routed events.

WPF Checkbox control donĀ“t react to MouseLeftButtonDown event

I have the following XAML code:
<Grid Background="Blue">
<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
<CheckBox MouseLeftButtonDown="CheckBox_MouseLeftButtonDown_1"></CheckBox>
</Grid>
</Grid>
When i click the checkbox with the mouse left button, the declared event is not fired.
Can anyone have an explanation for this behaviour?
Thanks in advance
The event is being handled by something else (probably being consumed by the Checked event).
If you change the event to PreviewMouseLeftButtonDown (the tunneling version of MouseLeftButtonDown), it will fire properly.
The CheckBox inherits from ButtonBase, which add a class handler for the left button down event (OnMouseLeftButtonDown). As the documentation for UIElement.MouseLeftButtonDown event mentions (emphasis mine):
Some control classes might have
inherent class handling for mouse
button events. The left mouse button
down event is the most likely event to
have class handling in a control. The
class handling often marks the
underlying Mouse class event as
handled. Once the event is marked
handled, other instance handlers that
are attached to that element are not
ordinarily raised. Any other class or
instance handlers that are attached to
elements in the bubbling direction
towards the root in the UI tree are
also not ordinarily raised.
You can play with the ClickMode and set it to ClickMode.Hover, which seems to prevent the behavior you are seeing. However, you might have to then maintain a custom logic in your event handler for this particular instance to set the proper toggle state of your check box.
You can also try the PreviewMouseLeftButtonDown event. However, marking that event as handled in your handler might have side effects on the rest of the MouseDown events - they will carry the handled information, which will prevent other instance handlers unless they are added with AddHandler with the flag for handling already handled events.
Franci thanks for your reply...I need to checkbox MouseLeftDownEvent to fire, and setting clickMode = hover solved my problem. I need MouseleftDownEvent to bubble the event to the parent grid.

Resources