Hi,
I have a listbox whose ItemSource property is bound to a ViewModel. This ViewModel has an ICollectionView property which is a collection of custom objects. I am loading this collection view as a tree structure in the Listbox. My requirement is to use the arrow keys to navigate along the tree view. I wrote the code to collapse an item in the tree, if it has children and is exapnded, when the left arrow is pressed. And a similar code on the right key press to expand the item. The issue here is, when the arrow keys are pressed, on selecting a particular listboxitem, the focus shifts from the selected item to the listbox and so when i try using the up and down arrow keys, the selection starts from the first item in the tree structure.
This is happening beacuse of the code that i am using to expand, collapse the item. Since the collection is a ICollectionView, i am try to modify the visibility of the items in the tree using the ICollectionView's Filter delegate. After this filtering is done, the focus is shifting to the Listbox control. But, in order to set the focus back to the item on which the key has been pressed, i am using the below code and explicitly trying to set the focus on the element:
for (int i = 0; i < box.Items.Count; i++)
{
object yourObject = box.Items[i];
lbi = (ListBoxItem)box.ItemContainerGenerator.ContainerFromItem(yourObject);
FormatItem fi = yourObject as FormatItem;
if (fi.Equals(item) && lbi.IsFocused)
{
break;
}
}
lbi.Focusable=true;
Keyboard.Focus(lbi);
Though i do this, i am not able to set the focus back to the item selected. Even i tried setting the selected item property of the listbox. Also, after the item is collapsed/expanded the line of code,
(ListBoxItem)box.ItemContainerGenerator.ContainerFromItem(yourObject)
is returning a null object, instead of returning the ItemContainer for that particular object.
Can someone please help me in identyfing the solution to this problem, its very urgent. Has it got anything to do with the ICollectionView that i am using to populate the Listbox?
Awaiting response. Thanx in advance. Sowmi.