c# - How do I access a control inside a XAML DataTemplate? -


i have flipview:

<flipview x:name="models_list" selectionchanged="selectionchanged">  <flipview.itemtemplate>           <datatemplate>                 <grid x:name="cv">                         <image x:name="img1" source = "{binding modelimage}" stretch="fill" tag="{binding modeltag}"/>                 </grid>            </datatemplate>   </flipview.itemtemplate> 

i want find img1 of selected index. while searching found method on post here:

private dependencyobject findchildcontrol<t>(dependencyobject control, string ctrlname)     {         int childnumber = visualtreehelper.getchildrencount(control);         (int = 0; < childnumber; i++)         {             dependencyobject child = visualtreehelper.getchild(control, i);             frameworkelement fe = child frameworkelement;             // not framework element or null             if (fe == null) return null;              if (child t && fe.name== ctrlname)             {                 // found control return                 return child;             }             else             {                 // not found - search children                 dependencyobject nextlevel = findchildcontrol<t>(child, ctrlname);                 if (nextlevel != null)                     return nextlevel;             }         }         return null;     } 

it returns me image on first index of flipview need 1 present on selected index.. tried edit method unable find required control. can me?

the problem experiencing datatemplate repeating , content being generated flipview. name not exposed because conflict previous sibling generated (or next 1 be).

so, named element in datatemplate have first generated item, , search inside generated item element want. remember, logical tree in xaml how access things name. generated items not in logical tree. instead, in visual tree (all controls in visual tree). means in visual tree must search control want reference. visualtreehelper lets this.

now, how it?

i wrote article on because such recurring question: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html meat of solution recursive method looks this:

public void testfirstname() {     foreach (var item in myflipview.items)     {         var _container = myflipview.itemcontainergenerator             .containerfromitem(item);         var _children = allchildren(_container);          var _firstname = _children             // interested in textboxes             .oftype<textbox>()             // interested in firstname             .first(x => x.name.equals("firstname"));          // test & set color         _firstname.background =              (string.isnullorwhitespace(_firstname.text))             ? new solidcolorbrush(colors.red)             : new solidcolorbrush(colors.white);     } }  public list<control> allchildren(dependencyobject parent) {     var _list = new list<control>();     (int = 0; < visualtreehelper.getchildrencount(parent); i++)     {         var _child = visualtreehelper.getchild(parent, i);         if (_child control)             _list.add(_child control);         _list.addrange(allchildren(_child));     }     return _list; } 

the key issue here method gets children, , in resulting list of child controls can search specific control want. make sense?

and answer question!

because want selected item, can update code this:

if (myflipview.selecteditem == null)     return; var _container = myflipview.itemcontainergenerator     .containerfromitem(myflipview.selecteditem); // same above... 

Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

How to call a javascript function after the page loads with a chrome extension? -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -