Transfer control to another wpf control during mousedown - wpf

This time I have a hard one for you :-)
Currently I am about to develop a usercontrol in wpf. I want this control to open up a slider as soon as one presses the mousebutton on a specific button within this control. The user should be able to move this slider as long as the mousebutton is still pressed.
As soon as the user releases the mousebutton the slider should disappear.
I already have a solution for this, but it's a very hacky one, because I am doing it through screen coordinates.
Is there any way to do this in a pleasant way with WPF?
Any help is highly apprechated!
Regards,
Michael

Please answer these questions first
What is your current XAMl and code behind?
While pointer of the slider is getting dragged along its length, do you want the button to be kept pressed all the time?
If so, did you use ToggleButton because it can be pressed throughout and can be toggled (unpressed) when you set its IsChecked property to false (i.e. when user releases mousebutton).
Have you checked mouse button state as Pressed (from button event args e) when mouse moves, being dragged, over some element such as the slider here?
e.g.
e.LeftButton == MouseButtonState.Pressed
EDIT
Related to point no. 2....
The reason why I suggest ToggleButton is because only that can be kept pressed while you slide on the slider. Otherwise with normal button, you can open the slider popup on button click but the button will loose its Pressed state when you move onto the slider.
Further, the IsChecked property of the ToggleButton can be two way bound to the IsOpen property of the slider popup. This way when the user stops sliding and releases the mouse, we can set IsOpen of the popup as false and that will automatically unpress the togglebutton due to that two way binding.

Related

Silverlight: How to route mouse events from one control to another?

If I catch mouse move/mouse button down events in one control, how do I route the caught event to another control?
In MSDIN documentation I found WPF UIElement.RaiseEvent but it seems it doesn't exist in Silverlight.
The reason for this question is the following issue.
I have an application where user is able to pick a control on the screen to retrieve control's ID (a custom property). While user picks a control, I don't want default actions of the control to be triggered - no button clicks, no text highlighting, no link navigation etc. That's why when entering the "picking mode", I put a transparent overlay over my application and after user clicks on it I find the element behind the overlay, get its ID and remove the overlay.
This approach is working fine except one scenario when there is a scroll viewer on the screen and user might want to pick an element which is scrolled out of view. Thus when picking elements, user at first clicks on a scrollbar to scroll the required element into view, but the scrollbar doesn't work because its behind the overlay.
Currently I have working code which detects if the element under mouse cursor (and behind the overlay) is a scrollbar instance, and thus I ignore it for my picking process - my application doesn't require picking scrollbars. But how do I pass the mouse event from the overlay to the scrollbar behind?
The short answer is, you can't route the mouse events.
But what you can do is: as long as the mouse is hovered over a Scrollbar you can set the IsHitTestVisible property of your mouseClick catcher overlay to false. The click will just go through it. Or can you only detect the Scrollbar the moment the user clicks?

MouseLeave of underlying object is fired even when overlapping object is IsHitTestVisible = false

I have created a custom popup to decorate my buttons with animated tooltips. I track Button.MouseEnter for the button to decide when to display the popup. I use Button.MouseLeave to determine when to hide the popup.
Problem is Button.MouseLeave is fired prematurely if the popup moves over the mouse cursor (its appearance is animated) despite the fact that I have set IsHitTestVisible = false for the popup and all its visual children.
Is this the way WPF is designed to work? I need MouseLeave to only fire when the cursor moves away from the button itself and not be influenced by the popup.
Thanks
I believe that the Popup control is actually contained within a window, which is why the popup can extend beyond the window bounds in some cases. (It's also why popup transparency is not supported in Silverlight.)
I believe that while the popup control is no longer processing "hits", the container window is, which is why you are losing your button's mouse focus.
I've not tested this, but you might try creating a template for your button and actually declaring the popup as part of the button (rather than below it). This may cause WPF to view the popup control as part of the button and eliminate the problem of losing mouse focus. This works in other scenarios, but I'm not 100% sure how this will work with a Popup.
EDIT: As a side note, the deault WPF tooltip allows you to override the template. I'm not sure what your goals are, but you may find it easier to change the appearance and behavior of the default tooltip than to try to roll your own, as a lot of these sorts of problems have already been solved in the default Tooltip.

allowing clicks on Button in ComboBoxItem when IsHitTestVisible is false

alt text http://img375.imageshack.us/img375/9830/combobox.png
Setting the IsHitTestVisible="false" has the effect of having the whole ComboBox's drop area unresponsive to clicks. The same goes for setting to true.
With a ComboBox I don't have to create any storyboard to make ComboBox animation like but I found this issue.
How can one make the ComboBoxItem area unclickable except for the button within?
Sounds like you should be creating a custom control that uses a popup window, that just looks like a combobox, however you can do what you wanted:
Put your buttons inside a grid, inside a single RadComboBoxItem
Set the grid background colour to 1% alpha so it is hit-test visible yet unseen
Add a Grid_MouseLeftButtonUp event handlern to the grid
In the handler set e.Handled to true so the mouse up is eaten
Make sure you close the combobox popup when you get your button presses.
Hope this helps.

WPF UserControl swapping and preserving keyboard focus

I've got a WPF window where I have a column of buttons on the left side. On the right side I am show/hiding UserControls as the left hand buttons are clicked.
I have created the UserControls once and then just switch between them with the buttons. As I switch I would like to retain the keyboard focus where it was when that UserControl was last visible.
In other words, I click on button A and show UserControl A. If I move keyboard focus to a textbox in that UserControl then click button B, do some work, then click button A again I would like focus to be on the same textbox that I last used in UserControl A.
Any ideas on how I accomplish this?
Declare a dictionary with the key being one the left-side buttons and the value the currently focused control. When a button is clicked, get the currently focused element and set it in the dictionary (with the key being the previously clicked button). Change the displayed UserControl and read the dictionary with the key being the just-clicked button. If there is a control for this entry, set the focus to it.
Use FocusManager.FocusedElement to know which control has the focus (actually an IInputElement, which should be the type of the dictionary value) and FocusManager.SetFocusedElement to put the focus back (or call Focus() on the control).

WPF Mouse behavior when ComboBox is focused

When the WPF ComboBox is clicked and in-focus, the only interaction that occurs after that can be with the ComboBox. If anything else is interacted with, including the window functions (minimize, restore, close, resize) and any control in the window, the action is ignored and the ComboBox loses focus.
In addition, MouseEnter and MouseLeave on the window buttons are still active, but when MouseEnter on the window border(?) occurs, the mouse pointer does not change to the resize pointer. This behavior makes sense because of the ComboBox's use of the popup control. The popup control exists independently of the main visual tree and if i.e. the window moves or gets resized, the popup remains fixed floating above the main window.
I have tried using Reflector, to see what the ComboBox is doing, but I have not been able to find what I am looking for. Basically, I do not know if this behavior is coming from the window, the ComboBox, or if it has something to do with the popup. How can I solve this problem?
You're right on in your description there, the popup keeps all action focus until it itself loses focus. If you're trying to change the functionality of the ComboBox you may want to look at creating your own ControlTemplate that behaves differently and does not keep the default action of the popup control.
Hope this helps, not entirely sure what you're trying to do.
I know this question is old, but for anyone coming here looking for the answer, it is to use Mouse.Capture.
The ComboBox sets Mouse.Capture(comboBox, CaptureMode.SubTree) in OnIsDropDownOpenChanged. This ensures that all mouse events are captured by the ComboBox. When the Popup is closed Mouse.Capture(null) releases the mouse capture.

Resources