c# - How can I programatically get keyboard focus on a WPF TreeViewItem? -
i'm trying programatically set keyboard focus tree view item (under conditions). i've tried 2 methods of setting focus, both of obtain focus on treeviewitem, lose keyboard focus.
the tree view bound view model:
<treeview name="solutionsmodel" treeviewitem.selected="solutionsmodel_selected" itemssource="{binding items, mode=oneway}" /> i'm trying set focus via treeviewitem selected routed event:
private void solutionsmodel_selected(object sender, routedeventargs e) { if (solutionsmodel.selecteditem != null && solutionsmodel.selecteditem solutionviewmodel) { if (e.originalsource != null && e.originalsource treeviewitem) { focusmanager.setfocusedelement(solutionsmodel, e.originalsource treeviewitem); } } } i'm trying set focus on treeviewitem in controltemplate:
<style d:iscontrolpart="true" targettype="{x:type treeviewitem}"> <setter property="isselected" value="{binding isselected, mode=twoway}" /> <setter property="template"> <setter.value> <controltemplate targettype="{x:type treeviewitem}"> <controltemplate.triggers> <trigger property="isselected" value="true"> <trigger.setters> <setter property="focusmanager.focusedelement" value="{binding relativesource={relativesource self}}"></setter> </trigger.setters> </trigger> <multitrigger> <multitrigger.conditions> <condition property="isselected" value="true" /> <condition property="isselectionactive" value="false" /> </multitrigger.conditions> <!-- <multitrigger.setters> <setter property="focusmanager.focusedelement" value="{binding relativesource={relativesource self}}"></setter> </multitrigger.setters> --> </multitrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> both of these methods focus, lose keyboard focus (treeviewitem.isselectionactive false). no other element in window has focus or keyboard focus can tell (in test, have 1 read textbox on panel focus). interestingly, can keyboard focus on (commented out) multitrigger isselectionactive false, of course forces keyboard focus on treeviewitem @ times.
is there way have better chance of getting keyboard focus, , conditions keyboard focus cannot obtained?
i'd add comment if but, why not have treeview handle focus , work item abstractly using treeview.selecteditem. tree view able know item selected when typing started. if item selected treeview in focus , can pipe keyboard commands through item.
Comments
Post a Comment