c# - WPF: Setting Focus To A Control Inside a ItemsControl -
i have itemscontrol
bound datasource , generates several containers. using code ensure 1 of containers visible @ given time. containers use templateselector
content of each container different (which rules out finding control name).
i set keyboard focus first control in visible container. have added event handler isvisiblechanged
event each container when access visualtree
there no children.
any ideas?
you can set focus on child element after child element has become visible. 1 option find child element want focus, add handler isvisiblechanged
. in handler, focus object , remove handler.
private void stackpanel1_isvisiblechanged(object sender, dependencypropertychangedeventargs e) { if (stackpanel1.isvisible) { uielement elm = stackpanel1.children[0]; frameworkelement fwe = (frameworkelement)elm; fwe.isvisiblechanged += new dependencypropertychangedeventhandler(fwe_isvisiblechanged); } } void fwe_isvisiblechanged(object sender, dependencypropertychangedeventargs e) { frameworkelement fwe = (frameworkelement)sender; fwe.isvisiblechanged -= new dependencypropertychangedeventhandler(fwe_isvisiblechanged); keyboard.focus((iinputelement)sender); }
edit: should able access items collection in isvisiblechanged event handler.
<itemscontrol name="sp1" isvisiblechanged="sp1_isvisiblechanged" itemssource="{binding items}" > </itemscontrol>
with code:
void sp1_isvisiblechanged(object sender, dependencypropertychangedeventargs e) { console.writeline(sp1.items.count); }
Comments
Post a Comment