WPF richtextbox selection problem - wpf

i have two richtextboxes one below the other in my application.when the user start selection in one richtextbox and continue to the other richtextbox selection should automatically move to the second richtextbox.is there any way to do this type of selection.
thanks in advance,
dhyanesh

You'd think you could use MouseEnter and MouseLeave, but when the mouse is captured (as it is during text selection), these events don't fire as expected.
The way to achieve your goal is:
Subscribe to MouseMove on the first RichTextBox.
In the MouseMove event, check Mouse.Captured to see if it is the RichTextBox.
If the mouse is captured, do a hit test on the mouse's current postion using VisualTreeHelper.HitTest. Go up the visual tree from the value of HitTestResult.VisualHit to see if the mouse is over a RichTextBox other than the current one.
If the mouse is over a new RichTextBox, cancel the mouse capture with Mouse.Capture(null), then fire a MouseLeftButtonDown event on the new RichTextBox to cause it to capture the mouse and begin selection.

Related

previewmousewheel not getting raised when mouse is not on the control in wpf

I have a combobox control's previewmousewheel event been handled. When my mouse is on the control and i move the center wheel of my mouse then this event gets raised. But when my mouse is away from the control and i move the center wheel of my mouse then this event does not get raised.
Can anybody please explain me why is this happening?
If I want to raise an event when my mouse is away from the control then which event should I handle?
This is expected behaviour.
If the mouse cursor is not positioned over the control then no mouse events are routed through it. You wouldn't expect a mouse click event to be routed through it if the cursor was over a different control would you?
If you want this behaviour then I would suggest that you handle the mousewheel event in the page/view and route it from there, be cautious though as user expectation is for mouse and keyboard events to be handled by the in focus item.
In those cases that the user would expect the event to be forwarded to the control you could use Mouse.Capture(someControl) and Mouse.Capture(null) to stop the forwarding.
You should only use this when it makes sense. E.g. when dragging a scrollbar, when you started to drag on the thumb but are not required to stay on top of it as long as you keep the left mouse button down.
When using Mouse.Capture() make sure you always provide a way back from capturing.

How do I capture the mouse when a Silverlight RepeatButton is depressed?

I have a RepeatButton on a UserControl that acts as the up button. The control is a number spinner of sorts. When the number value goes from 9 to 10 the up button is released because it moves out from under the mouse. I know I can capture the mouse, but my RepeatButton is not giving me MouseLeftButtonDown events. So how do I capture the mouse over the RepeatButton when it is depressed and release the capture when it is released? And should the RepeatButton give me MouseLeftButtonDown events?
Edits:
It would seem that the template content is stealing the button's MouseLeftButtonDown events. Is there anyway I can circumvent the button's content. If I set HitTestVisible to false, the button itself becomes untouchable. I wish silverlight had OnPreview overrides.
Have you tried utilising MouseLeftButtonUp instead to check if it can occur when the mouse button is released?
I just had a similar problem with Windows 8/WinRT and found a workaround - you can bind something to the button's IsPressed property and when it gets set to false - it means the button/touch is up.

Highlighting control when mouse is over child control

I have a silverlight templated control that changes opacity when you hover it . However when user points cursor to its child control the effect wores off. I want to have the control highlighted also when the user hovers any child control. I've did the same thing in WinForms by overriding the WndProc method. Is there something similar in silverlight ?
Thanks
Sounds to me like you have not used the correct events to detect the hover, I suspect you are using MouseMove. Instead Use MouseEnter and MouseLeave events. An MouseEnter event will occur when the mouse moves over the control. You move the mouse over child controls and you will get no further events. Then when the mouse moves completely out of your control you will get MouseLeave.

Mouse events between two ItemsContorl elements

Take a look at below code. BoxControl has MouseLeftButtonDown/MouseLeftButtonUp events and those work fine when the mouse is clicked on
the box control other than when the mouse is clicked on below text block. I want MouseLeftButtonDown/MouseLeftButtonUp events of BoxControl
to work when the mouse is clicked on below textblock. Appreciate your help!
It works as expected. Your BoxControl is the one which has the click events so that is the only control which is listening for these events, and TextBlock isn't.
There are two things you can do:
1) Add the TextBlock in your BoxControl
OR
2) Add the MouseLeftButtonDown and MouseLeftButtonUp events on your current TextBlock and get these textblock_MouseClickHandlers to delegate to the event handlers for the BoxControl
You should hook into the PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp events instead. Make sure to mark them as handled if you don't want them to bubble up any further.

Giving a WPF treview to do Richtext (Xaml) displaying and editing and drag drop

To give the ability to display and edit Richtext (Xaml) inside a treeview item,I have used a bindable Richtextbox (custom user control that used a richtextbox) to the HierachicalDataTemplate that used to populate the TreeViewItems.
This works fine but at the same time I want to have the drag and grop functionality in the treeview. My problem is when I click on the richtextbox (inside the treeviewitem) and do a drag it will do the richtextbox content dragging. What I want is to drag the treeviewitem insted (fire the drag and drop events of the treeviewitem).
NOTE: If I used a textblock this can be done but can't display richtext. Any Help?
Trigger your drag from the on the "Preview" mouse events rather than the regular ones, and set the Handled flag in PreviewMouseMove when doing a drag so the RichTextBox never sees the MouseMove.
For example, you might record the mouse down location in PreviewMouseLeftButtonDown, then if you receive a PreviewMouseMove with the left button still down you can set e.Handled=true and then call DoDragDrop in a dispatcher callback.
The reason this works is that RichTextBox uses the regular (non-preview) mouse events.

Resources