wpf TreeView - select row on click anywhere in row - wpf

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.

Related

programmatically making a dropdown menu in vb.net WPF

I'm making a custom dropdown button (since the one included in wpf requires too much hacking to style right). Now that i got the button bit out of the way i need to add the drop down part.
My first thought was to add a stackpanel and use that to contain the items but it gets cut off if it leaves the borders of the grid that the button is in. Next up was the popup primitive, it gets on top of everything nicely enough but position wise it just free floats and i haven't figured out how to make it follow the button it was spawned by. I also tried using contextmenu but that seems to have no positioning controls at all and just sits where the mouse made it..
Anyways wpf is a big package and I'm just getting into it, anybody know which direction i might find what I'm looking for?
Preferred approach normally is to use a Popup. You got two very important properties with a Popup
PlacementTarget and Placement
Setup a binding for PlacementTarget on the Popup to your custom Button and then use Placement to position the Popup accordingly w.r.t to the PlacementTarget(Button)
Placement accepts an enum of type PlacementMode which gives you quite a few options to position the Popup.

ListView - Selected Item in the middle

I'm building a Windows Store app using XAML. I have a scrollable ListView that has its SelectedItem set on start.
Now, I have two requirements:
I would like the selected item to be always visible
In addition it
should be displayed in the middle of the list when possible (it's
not possible for the first element).
The first requirement I can partially fulfill by using list's LayoutUpdated event and list.ScrollIntoView(item) method, but this doesn't allow me to scroll down the list manually until the item is unselected.
For the 2nd requirement I have no ideas.
Ok, found out how to achieve that: http://fczaja.blogspot.com/2013/05/xaml-listview-scroll-selecteditem-to.html

ListView item highlighting

I am designing a code viewer using a virtualized ListView control to display code lines.
Now I want to create a highlight effect when the user clicks a link which takes them to a particular line. I want the target line to be highlighted.
The effect will be either an "underline" appearing (and disappearing) or a semi-transparent overlay (like a marker pen) appearing (and disappearing). The actual graphical effect itself is unimportant, that's not the problem.
What is the best approach to achieve this? I'm not quite sure where to start.
Some technical requirements might be that I need to:
find the right events to react to - or use databinding
obtain the absolute bounding rectangle of the virtualized target item (although when brought into view the item should be available)
absolutely position a canvas effect on top, fade in and fade out
...Ideally some state changes in a view model, a piece of XAML is animated via a triggered storyboard to appear (fade in) above the relevant ListViewItem and then animates away again (fade out). Of course before the fade in, the element must already be correctly positioned over the relevant ListViewItem...
I have worked on a search feature for a Listview where every ListViewItem had few textbocks. When the user types something in a search textbox, all the matches in the listview was supposed to get highlighted.
I created Run objects based on the search string (used Regex to find the match) and then set the Background to some color. Also, held the reference of the ListViewItem in the tag of the Run object which helped me to use call ScrollIntoView. Hope this will be of some help in your case.

WPF Adorner Overlay Problem

I am trying to achieve functionality similar to that of a Popup, without using a Popup, but instead adorning my ContentControl with a basic adorner. Basically, I want the ContentControl to have an "overlay" effect, whereby it is the topmost object, above all other elements - similiar to that of the Popup control.
Here is the problem that I am running into, and I am hoping that someone can point out where I am going wrong:
I have a stand grid with two row definitions. The first row contains a UI element - for example, a rectangle. The second row contains a custom control that I have developed to emulate the functionality of a "drawer" sliding out. Basically, when I click on button, I am going to animate a TranslateTransform to "slide" my ContentControl "up". This works fine - except that it gets cropped underneath the rectange in the first row of the grid. If I remove the row definitions in the grid, then when the desired behavior is achieved - the ContentControl is moved "up" and partially "on top" of the rectangle. The rectangle is merely a place holder for what I am trying to achieve. I basically want to have a drawer type control that can slide out and be on top of all other controls.
I am somewhat new to using the Adorner class, so, I am hoping that someone can please point out where I am going wrong.
Thanks.
Chris
Change the parent of the adorner to the full grid, and not just your control. If you put a control in a grid row, and set the adorner to adorn the control, it will usually be clipped to that row because the control is.

Determine who has focus in WPF Window

We are using WPF and have a window derived from a DockingLibrary. This window has a grid with multiple items in it, one being a WPF datagrid. We are using the M-V-VM pattern. When this windown is created and shown, none of the rows in this datagrid are selected. We can set the row to display as highlighted by doing something like:
SharedWindow.ShipmentWin.shipmentDataGrid.SelectedIndex = 0;
This causes the first row in the datagrid to be shown as highlighted. But, and isn't there always one of these, this row is not Selected nor does it have Focus. I tried setting IsSelected and Focus on this row as in:
SharedWindow.ShipmentWin.ShipVM.IsSelected = true;
bool focused = SharedWindow.ShipmentWin.shipmentDataGrid.Focus();
Am I going about this all wrong and is there some other way to Select the first row in the datagrid and set focus to it? Typically, when a datagrid is created, no row is selected until the user mouse clicks on the desired row.
Any thoughts would be greatly appreciated.
thanks!
Have a look at the FocusManager. It allows you the set the focus to another UI element via the SetFocusedElement method. Additionally, it allows you to determine the currently focused element in your application which is can get handy to debug focus issues. Snoop can be useful, too. It shows the currently focused element in the bottom status bar.
If you use the DataGrid from the WPF Toolkit, be prepared to find some bugs in relation to focus handling. Some have been addressed recently.
It's also worth understanding the difference between logical focus and keyboard focus, which are quite different animals. The .Focus() method sometimes only sets logical focus - which probably isn't what you want.
The documentation for the Focus method tells you that it will return true if the keyboard focus was set, and false otherwise.

Resources