This one is a bit tricky to explain.
I have a usercontrol with some textboxes. I also have a menu just above this usercontrol in the same window. Whenever I tab away, the LostFocus fires correctly on the textbox, and this is what I want. Strangely enough, if I click the Menu button on top of my window, the LostFocus event does not fire on the textbox. Is there an elegant way to make sure that my menu properly allows LostFocus to fire on any controls which last had focus?
I also want to avoid having to Update BindingExpressions otherwise I would likely be doing this for N textboxes, which is undesirable.
I can't imagine it is too difficult to achieve this.. I just don't understand how this doesn't work: in most other situations LostFocus always fires.
Any ideas? Thank you.
Is the menu WPF as well or Winforms / UnManaged? If either of the two then the lost focus event does not fire. This can play havoc with WPF controls as many time a save or other data function is being performed from the menu. To counter this I have had to implement multiple ways to combat this. The easiest way was to implement a mouse leave event on the user control itself and perform any actions you require manually in code.
Related
I've got a TreeList that's basically like a Photoshop layers palette. It's a hierarchical list with checkboxes to toggle visibility of a document's individual layers. This is done via the BeforeChecked event, which is raised right before the .Checked value toggles.
It works fine, except if you double-click it, at which point it all seems to go haywire.
If you double-click a checkbox once, it toggles the checked value twice (which is the intended behavior), but it doesn't toggle the visibility of the layer twice because it doesn't raise the BeforeChecked twice.
I figured I'd get around this by putting this in the MouseDoubleClick event:
TreeViewHitTestInfo hit = treeLayerPalette.HitTest(e.X, e.Y);
hit.Node.Checked = !(hit.Node.Checked);
This works for all double-clicks except for the first one. So it only raises the BeforeChecked event once (and not the MouseDoubleClick) at first, getting the checkbox out of sync with the visibility of the layer, and then all following double-clicks raise both the BeforeChecked and MouseDoubleClick events (which in turn raises the BeforeChecked event), maintaining that incorrect relationship.
Also, at one point, I put a MessageBox.Show() in the DoubleClick event. Awkwardly enough, it does not actually get shown on a double-click, but instead gets shown on a third click, no matter how much time has elapsed between the actual double-click and the third click. A third click performed 20 seconds after a double-click will raise the MouseDoubleClick event, but the actual double-click won't.
What's actually going on here, and how can I fix it?
this is a problem with Checkbox Enabled treeviews, however there are a few acceptable workrounds..
Firstly: MS know about the problem but refuse to fix it... : http://connect.microsoft.com/VisualStudio/feedback/details/775922/treeview-double-click-bug#details <-- The Bug report....
So there is no way but to workaround it.. simplest been to subclass the Treeview and forcibly disable the dblclick on the checkbox...
answer (on SOF) : c# treeview ignore double click only at checkbox
Hope this helps....
I have an application in Silverlight and WPF. The error just happens in Silverlight, but same code is used in WPF.
In my application, there is a RibbonBar, with several RibbonGroups. In each RibbonGroup there are at least one RibbonButton. One of those RibbonGroups also contains four TextBox.
Every TextBox has its own OnLostFocus-Handler. When I leave a TextBox the related Handler is raised.
Now, (1) I click into one of those TextBoxes and (2) then click a RibbonButton, OnLostFocus raises and after that the RibbonButton dropdown menu appears. Everything OK.
After that, (3) I click on another RibbonButton. Again OnLostFocus raise, although - imho - it shouldn't.
So it goes on and on. After every action the TextBox keeps(or gets back, don't know) the focus and this causes the OnLostFocus-Handler to raise.
In WPF same code does not raise OnLostFocus again. Just one time, when it really lost focus.
Does anybody know, why this behaviour is that strange in Silverlight. What is really different to WPF, maybe I just have to set a property which default value ist different to WPF.
Thanks in advance.
Try using OnPreviewLostKeyboardFocus. OnLostFocus is for logical focus which you may lose for a variety reasons.
I don't know the reason for sure but I suspect that the problem you're seeing might be because the ribbon bar is in a FocusScope. When you put focus onto something inside of a focus scope then what you end up with can seem like focus bouncing around some. I'm not certain of this answer, focus is very complicated in WPF. Submitting some code samples might help.
My form has several textboxes and several buttons. The textboxes are loaded with data by the load event. If I make changes to the textboxes and then move the cursor over a button, the textboxes are suddenly loaded again with the original information. I don't click a button. I just move the mouse over one. Why is this happening and how do I stop it?
This cannot happen by itself. I suggest you check all event-settings.
For instance, you could have, by accident, linked the Load event to the Button's OnMouseEnter or something like that.
After your comment:
You should absolutely not use the paint event to initialize things. The paint event will be called after every change in the Form.
So move that code to the Load event.
In my C# app, I have a ListView on a Form. I want the user to be able to double-click on a section of the ListView when no items are selected in order to pop up a "New Item" dialog. The problem is that the DoubleClick event for the ListView only fires if an item is selected.
Is there a way to do this?
There is a way to do this, but you have to do some low-level drilling into the Windows machinery. It's generally not a good idea to spend a great deal of time trying to get a standard Windows control to behave in a non-standard manner.
A simpler way is to just put a "New Item" button next to your ListView. If screen real estate is an issue, you could just add an extra row at the bottom that says "{click here to add new item}", and show your dialog when the user clicks this last row.
Add an event handler for the List view's MouseDoubleClick event.
Assuming Windows Forms:
Perhaps a good workaround would be to use a ContextMenu.
I've built several user controls in WPF and they all get added to a canvas. I want to add a behaviour that keep tracks of the currently selected usercontrol. A usercontrol should be selected when:
The mouse clicks on it;
when it recieve focus;
when either of the two above happens to a subcontrol of the usercontrol.
Is there any way to handle this purely by using the focus mechanism of WPF or will I need to take care of this myself with assistance of the focus classes?
I've read up upon the new way of handling focus in WPF, and the problem I'm facing is that the keyboard focus determines what the currently selected object is, but some parts of the my control can't recieve keyboard focus so even though these parts are clicked, the usercontrol doesn't recieve focus.
I'm looking for advice on how to implement this feature and how much I could/should rely on the focus mecanisms. Ultimatively I wouldn't mind if only a single object could be selected, but if it's easily extendable to multi-select then I wouldn't mind this either.
Just to clarify, I know I could build this manually by handling a lot of events and keeping track of states, but I was just hoping an easier approach was available.
Combine UIElement.IsKeyboardFocusWithin with a PreviewMouseDown handler:
When PreviewMouseDown is called, set a flag and schedule callback using Dispatcher.BeginInvoke at DispatcherPriority.Input to set focus to the UserControl if the flag is still set.
Set a handler for UIElement.IsKeyboardFocusWithin property changes in your UserControl. When the handler fires, clear the flag.
The idea here is that if you click anywhere on the UserControl and keyboard focus does not result in the focus being moved into the UserControl, force it into the UserControl.
FYI, here's roughtly what step 1 looks like in code:
public override OnPreviewMouseDown(MouseButtonEventArgs e)
{
_mouseClickedButNoFocus = true;
Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
{
if(_mouseClickedButNoFocus)
Focus();
});
}
You could use the UIElement.IsKeyboardFocusWithin property, which is true when the UIElement or one of its children has keyboard focus. It is a dependency property, so you can easily use it for a trigger in a style
You can set the logical focus to the control when any of the child gets the keyboard focus using FocusManager.IsFocusScope="True". Setting the keyboard focus to the control or trying to do will eat the keys for the child controls.
You can use UIElement.IsKeyboardFocusWithin to set the focus of the control if any of the children has focus.
You can read this article which I think describes the difference between Logical and Keyboard focus quite well:
http://www.pluralsight.com/community/blogs/eburke/archive/2009/03/18/why-is-focus-in-wpf-so-tricky-managing-and-understanding-focus-in-a-wpf-application.aspx