wpf - Focusing with FocusManager, ContentControl and DataTemplate -
i'm trying (keyboard) focus work application of next structure:
itemscontrol datatemplate item contentcontrol datatemplate content (multiple, depending on type) container different controls (mainly textbox or ok button should have focus)
on root of each content template ("some container"), set focusmanager.focusedelement
1 command within template. of items in itemscontrol, 1 of contentcontrols visible (the "current" one). so, basically, create first thing visible, ok button in has focus, user can press come in confirm. 1 hidden, next 1 shown, , main command (e.g. textbox) has focus, user can press come in confirm (small event handling create come in in textbox confirm item) , on.
when done, user can click button in toolbar start over, making first contentcontrol visible again. time doesn't have focus. don't know has focus, i'd work in beginning. tried set focus just-shown contentcontrol (which works, visible through dashed line), won't set focus it's children. since don't know of info templates chosen (well, not without writing special code), can't set focus manually or through binding child. maybe i'm missing simple thing @ point?
on other hand, when user manually clicks on active contentcontrol button or textbox, can confirm enter, itemscontrol seems have focus, rather next item. can't head around focus thing. there simple solution? have write code traverse tree contentcontrol right datatemplate? , then, can focus somehow datatemplate or command set focusedelement, or have write code each datatemplate separately?
you should implement action can triggered viewmodel focus element when needed. seek code:
using system; using system.collections.generic; using system.linq; using system.text; using system.windows.interactivity; using system.windows; namespace behaviors { /// <summary> /// action focus target element, if invoked when target not yet specified, focus 1 time target set /// </summary> public class focusaction : targetedtriggeraction<dependencyobject> { protected override void onattached() { base.onattached(); } protected override void ondetaching() { base.ondetaching(); } protected override void invoke(object parameter) { if (target uielement) { if(!((uielement)target).iskeyboardfocuswithin) ((uielement)target).focus(); } else invokeonconnect = true; } private bool invokeonconnect = false; protected override void ontargetchanged(dependencyobject oldtarget, dependencyobject newtarget) { base.ontargetchanged(oldtarget, newtarget); if (invokeonconnect) { invokeonconnect = false; invoke(null); } } } }
and in wpf:
<dockpanel> <i:interaction.triggers> <i:eventtrigger eventname="loaded"> <nuib:focusaction targetname="title"/> </i:eventtrigger> <textblock x:name="title"/> </dockpanel>
namespaces are:
xmlns:nuib="clr-namespace:behaviors;assembly=nui" // action class xmlns:i="clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity" // .net library interactivity
wpf focus datatemplate focusmanager
No comments:
Post a Comment