wpf - Focus first textbox in some ListBox item -
i have listbox
listbox.itemtemplate
contains labels , textboxes.
i want focus first textbox of last item programatically. how can that?
i don't know how can access textbox object created dynamically data binding.
try use behaviour (code not mine - https://gist.github.com/tswann/892163)
using system; using system.windows; namespace myproject.wpf.helpers { /// <summary> /// allows arbitrary ui element bind keyboard focus attached property. /// </summary> public static class focusbehaviour { public static bool gethaskeyboardfocus(dependencyobject obj) { return (bool)obj.getvalue(haskeyboardfocusproperty); } public static void sethaskeyboardfocus(dependencyobject obj, bool value) { obj.setvalue(haskeyboardfocusproperty, value); } // using dependencyproperty backing store haskeyboardfocus. enables animation, styling, binding, etc... public static readonly dependencyproperty haskeyboardfocusproperty = dependencyproperty.registerattached("haskeyboardfocus", typeof(bool), typeof(focusbehaviour), new uipropertymetadata(false, null, coercecurrentvalue)); /// <summary> /// coerce property value , give focus bound control if true. /// </summary> /// <param name="source">uielement property has been attached.</param> /// <param name="value">property value</param> /// <returns>property value</returns> private static object coercecurrentvalue(dependencyobject source, object value) { uielement control = source uielement; if (control != null) { bool hasfocus = (bool)value; if (hasfocus) { system.threading.threadpool.queueuserworkitem((a) => { control.dispatcher.invoke(new action(() => { control.focus(); })); }); } } return value; } } }
Comments
Post a Comment