TreeListView - Focused column

Grids for WPF Forum

Posted 6 years ago by Miguel A.
Version: 18.1.0672
Avatar

Hi,

is there a way to determine the focused column (index) in TreeListView?

Comments (6)

Answer - Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Miguel,

We don't have anything built in but you might be able to find a visual ancestor of the focused element that is a TreeListViewItemCell, and that has a Column property on it.  You can look at the Column property to get the related TreeListViewColumn.


Actipro Software Support

Posted 6 years ago by Miguel A.
Avatar

Hi,

thanks for the answer but although the TreeListViewItemCells I get are correct I don't know which one is focused, because they all have IsFocused set to false makes me impossible to get the focused column.

public static TreeListViewColumn FocusedColumn(this TreeListView tree)
{
	return FocusedItemCellFromItem(tree, tree.SelectedItem)?.Column;
}
public static TreeListViewItemCell FocusedItemCellFromItem(this TreeListView tree, object item)
{
	if (tree == null || item == null) return null;
	var container = tree.ItemContainerGenerator.ContainerFromItem(item);
	return container?.FindItemCell();
}
static TreeListViewItemCell FindItemCell(this DependencyObject depObj)
{
	if (depObj == null) return null;
	for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
	{
		DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
		if (child is TreeListViewItemCell cell)
		{
			//always set to false	
			if (cell.IsFocused)
				return cell;
		}

		var childOfChild = FindItemCell(child);
		if (childOfChild != null)
			return childOfChild;
	}
	return null;
}

 

 It seems to be possible to get the focused column on previewmousedown event where it can be stored for later access. But this leads to several issues, such as what happens if the user selects another row / cell via keyboard? Thus its not exactly what I was looking for. Getting the focused column at any time from selecteditem I have no success with. Maybe someone can help?

 

private void treeListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
	var dep = (DependencyObject)e.OriginalSource;
	while (dep != null && !(dep is TreeListViewItemCell) && !(dep is TreeListViewColumnHeader))
	{
		dep = VisualTreeHelper.GetParent(dep);
	}
	if (dep == null) return;

	if (dep is TreeListViewItemCell cell)
	{
		var focusedColumnIndex = cell.Column.Index;
	}
}

 





[Modified 6 years ago]

Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Miguel,

I would need more information on your setup to be able to suggest anything.  Normally the TreeListView is basically like a normal ListView where an entire row (TreeListViewItem) is selected.  While it can be used like a grid (with focusable controls in column cells), that isn't the normal use case.  My first reply assumed that you have focusable controls in your cells.

If you do have focusable controls like TextBox in each cell, then I was suggesting doing something like getting Keyboard.FocusedElement and walking up its visual tree until you hit TreeListViewItemCell.  Then looking at that cell's Column.  But that tip only works if you have focusable controls in the cells and one currently has focus.

Also in reference to your reply, IsFocused is something on UIElement.  Since the cells themselves are not focusable, that will never return true.

If that's not what you're looking for, please give more details on your setup.


Actipro Software Support

Posted 6 years ago by Miguel A.
Avatar

Hi,

my columns are added on runtime on user's command. Every treeitem has an array holding the values of the columns (this seems to be needed because in my case every column is having its own data. You read between columns instead of between rows). The columns only show text via displaymember binding. The user needs to be able to get or modify information from a specific cell, either via keybinding / context menu or double clicks. With selecteditem I get the treeitemmodel but I would also know the focused column to get the index.

Treeitems need to be expandable thus a datagrid is not an option.

var col = new TreeListViewColumn
	{
		DisplayMemberBinding = new Binding(nameof(TreeItemModel.Values) + $"[{treeList.Columns.Count - 1}]")
	};

 Perhaps only my approach is wrong and there is something else I can do?

[Modified 6 years ago]

Answer - Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Miguel,

I think the confusion is that TreeListView works the same as a normal ListView and ListBox, where each "item" is technically a row.  And only the full row is really selectable.  There is no concept of focus being in a particular column or cell.  That concept is what you find in a more advanced grid control.  When you press up/down arrow in TreeListView, you are selecting the previous/next item (i.e. full row).

If you used a DataTemplate for each TreeListView cell that had a control with a tab stop instead of DataMemberBinding, you might be able to move focus around more within the cells that way.  And doing some of what I mentioned above would possibly help too (using Keyboard.FocusedElement and walking up the tree to find the containing cell).  But we haven't really played with that concept ourselves.


Actipro Software Support

Posted 6 years ago by Miguel A.
Avatar

Thank you, I'll give it a try.

The latest build of this product (v24.1.2) was released 2 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.