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.
Related
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.
I am developing a custom control (say BoxControl) which will have many controls in it like a textbox, few buttons etc.
I will have many BoxControls in a row and while navigating via tabs, I want it to behave like when a BoxControl gets focus, it always passes the focus to its textbox and when its textbox loses the focus, the entire BoxControl loses the focu and passes the focus to next BoxControl.
Any ideas how can it be done?
You will need to add an event handler to your BoxControl to handle the GotFocus event and then put the focus on its text box.
You will need also to add an event handler to the LostFocus event of the textbox and then you can raise a custom event on BoxControl so it's controller can know that has to pass the focus to the next BoxControl
Hope it helps.
You can set Focusable property to false by a Setter in your custom control's template. Both on the control and on the various elements inside.
I have a search screen in my WPF application. The screen is implemented as a UserControl in a TabItem of a TabControl. When the user switches to the Search tab, I want the focus to go into one particular field.
So I added a Loaded event handler to the UserControl tag in the Xaml and I called the Focus method of the control I want to have the initial focus in the Loaded event handler. This worked great until I upgraded the Telerik control library I'm using today. Now, when I switch to the Search tab, the focus is NOT in the field I want to have it, but I can't tell what control does have the focus.
The field I want to have focus already has GotFocus & LostFocus event handlers for other reasons. I remembered that in Win Forms, the LostFocus event handler arguments tell you which control is going to get the focus. So I put a breakpoint in my LostFocus handler & discovered that the arguments to the LostFocus event handler in WPF don't include that information.
How can I figure out where the focus is going without putting GotFocus handlers on every control in my UserControl?
Tony
You can try putting your breakpoint on the LostKeyboardFocus Attached Event instead of the LostFocus Event. It uses the KeyboardFocusChangedEventArgs Class which does have properties that show which element had focus and where the focus is going.
private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
textBox1.Text = ((FrameworkElement)e.NewFocus).Name ;
}
Try to press Tab Key and see if it helps you find the control in focus.
You can also use Snoop as suggested in this Q/A: Any tips on debugging focus issues in WPF?
For starters, Snoop shows the current focused element and the current
FocusScope in the status bar.
You can get it to show you all the GotFocus and LostFocus events:
1. Run your app.
2. Run Snoop.
3. Choose your app in the dropdown.
4. Click the binoculars ("Snoop") button.
5. On the right pane, click the Events tab.
6. Click to bring down the dropdown.
7. Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
8. Now do what you need to do to manipulate focus and watch the Snoop window.
Similarly you can track the FocusManager events the same way.
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.
I want to get notified when I click a window in WPF (I use the GotFocus event), but it only triggers when I click on a Combobox in the window. What I want is to get notified when the the window or any of the controls in the window is clicked. Any ideas of how to do this?
The GotFocus event doesn't get fired when the Window gets focus, it's intended to be used with controls only. The Activated event serves this particular purpose.
Use Activated event instead of GotFocus.
You could try the IsKeyboardFocusWithinChanged event. It should trigger when the keyboard focus is taken by an element of the window, or when it is taken by another window