WPF Expander MouseOver Issue - wpf

I have Expander control which shows list of items when you expand it. If i do mouse hover over on header, it will expand it.
I need following things which needs to be implemented from code behind or in xaml.
1) if user do mouseover on expander header and press enter key then by default first item from the list needs to be selected.
2) if user do mouseover on one of the item of list and press enter key then we have to select perticular item. - I already have this implemented.
My question is I am able to capture enter key event in code behind but how would I know that user has mouse over on expander header or expander item as I have to do two diff things.
Thanks
Dee

You can use Mouse.DirectlyOver to get the element it's over, you can then walk up the tree to see if you are in your relevant scope (i.e. find the expander/header), or you can check the IsMouseOver of the relevant controls instead, which should also return true if the mouse is over a child element.

Related

ToolStripControlHost selection on mouseover

I have a problem developing a custom menu item.
The menu item is hosted in a ToolStripControlHost, the rest are just ToolStripMenuItems.
What I want is that when the user hovers the mouse over the custom control, to be selected like an ordinary ToolStripMenuItem, like in the image below. Unfortunately, I did not manage to figure this out. Is this behavior possible?
Edit: 26.10.2015:
I have been playing around with the control and it seems that the ToolStripControlHost, when added to the ContextMenu it is shrinked in the list, like in the image below.
I made the background of the custom control red so it can be seen better. The control only receives MouseHover events only when the mouse is over the control, shown in red. If you point the mouse on the left or the right, the ContextMenu receives the events.
ContextMenu custom menuitem
It is an interesting behaviour, because also the ToolStripTextBox or ToolStripCombobox don't take the whole left to right space and are not highlighted when mouse is over.
Now I hope someone has an idea how to do that.

wpf TreeView - select row on click anywhere in row

The wpf TreeView only selects a row when the text if clicked on. I'd like to have the selection occur no matter where the click occurs in the row. Except perhaps on the expander I guess but would have to see.
Currently I stretch the text to the width of the treeview using the technique posted here:
https://stackoverflow.com/a/15813560/317033
However that leaves the empty space on the left of nested child nodes still unreactive.
https://stackoverflow.com/a/672123/317033
Based on reviewing this answer I modified the ControlTemplate of the TreeViewItem replacing it with my own version which filled the width of the TreeView. It'd be nice if there was another way since this obviously is a major change.
Handling a click in the TreeView was my previous thought, but I couldn't find a way to get the item for that click if the click occurred to the right or left of the item.

silverlight 3 listbox item highlight versus selected

I have a listbox and am attempting to select and item in code. Sometime one item is highlighted, that is it is background is colored blue, but a different item has a square blue box around the it (no highlighting just an hollow outline of a box).
Am I correct in saying one is "highlighted" and one is "selected" and do I have them correctly identified?
Should this be happening... that is these 2 things being out of sync?
Thanks
Cody
Just fought this issue.
Although the listbox scrollviewer will auto scroll too the selected item in the listbox the first item would have focus and as you discribe keyboard interaction then operated with the first item in the list not eh selected item.
For us the fix was
this.MyListBox.UpdateLayout();
this.MyListBox.Focus();
this.MyListBox.SelectedItem = MyObject;
this.MyListBox.ScrollIntoView(this.MyListBox.SelectedItem);
The order of the actions seems very important.
This was using the ListBox internal scrollviewer.
The item with the blue highlighted background is the SelectedItem. The item with the blue rectangle is the item that currently believes it has the focus.
Ordinarily the Focus rectangle the selected fill are found together because the selected item usually changes with a mouse click which also brings the focus to the same element. However its possible for example that code may change the selected item whilst the ListBox still has the focus. In that case the selected highlight will move the newly selected item but the focus rectangle will remain where it is. (Note to the pendatic I'm describing what appears to the user not how things actually work under the hood).
For an insight on what is going on see the ListBoxItem style in the ListBox Styles and Templates documentation.

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).

Incorrect item gets selected when starting a drag on an item in an ItemsControl

I am implementing a DragDrop framework for WPF (which incidentally you can find here).
I have a problem in that when the user MouseDowns on an ItemsControl we don't know immediately if they intended to click the item to select it, or to begin a drag. If the user clicks an item and then quickly moves the cursor, another item other than the clicked item can become selected before we determine that a drag has started (especially if clicking on the item freezes the UI for a short time to load data etc).
I think this problem didn't exist in WinForms as dragging the mouse with the button held down didn't cause another item to become selected - the selection was made only on the item on which the click occurred.
In the PreviewMouseDown event I can set the e.Handled property to prevent another item becoming selected, which works fine if the user actually did intend to start a drag, but then they can't actually select the item.
Anyone know how to handle this?
Store/use the point at which they originally moused-down, and use that to resolve the item to drag.

Resources