c# - Change the Foreground Color of List View Selected Item from Code Behind in WP -


i trying change foreground color of list view code behind getting object reference not set instance of object exception.
here code;

var item = listviewtest.selecteditem; listviewitem listviewitem = this.listviewtest.containerfromitem(item) listviewitem; listviewitem.foreground = new solidcolorbrush(colors.greenyellow);  //manually scrolling selected item listviewtest.scrollintoview(listviewtest.selecteditem); 

as can see code, want change foreground color e.g yellow , scroll particular listview item. scrolling works foreground color isn't working , getting exception.

update

here item template;

            <listview.itemtemplate>                 <datatemplate>                     <stackpanel margin="0,0,0,9.5">                         <textblock                             fontfamily="times new roman"                             text="{binding id}"                             horizontalalignment="right"                             pivot.slideinanimationgroup="1"                             commonnavigationtransitioninfo.isstaggerelement="true"                             style="{staticresource listviewitemtextblockblackstyle}"/>                         <textblock                             text="{binding fullinfo}"                             horizontalalignment="right"                             pivot.slideinanimationgroup="2"                             commonnavigationtransitioninfo.isstaggerelement="true"                             style="{staticresource listviewitemsubheadertextblockblackstyle}"/>                     </stackpanel>                 </datatemplate>             </listview.itemtemplate> 

update 2

here debugger shows containerfromitem null enter image description here

the reason why listviewtest.containerfromitem(item) returning null because

  1. container not rendered yet
  2. no item found
  3. container item not visible in listview yet (maybe need scroll see item)

solution

before call listviewtest.scrollintoview(listviewtest.selecteditem);

call await system.threading.tasks.task.delay(1); let listview load first. call scrolltoview()

another solution add item can access container listviewtest.items[listviewtest.selectedindex] , set forecolor there

edits

to add item manually loop trough item , call method.

    private void additem(foo f)     {         listviewitem lvi = new listviewitem();         stackpanel sp = new stackpanel();         textblock tb_id = new textblock();         tb_id.text = f.id;         // set other proerty here         sp.children.add(tb_id);          textblock tb_fullinfo = new textblock();         tb_fullinfo.text = f.fullinfo;         // set other property here         sp.children.add(tb_fullinfo);          lvi.content = sp;         listviewtest.items.add(lvi);     } 

and of course need set other properties font family , such.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -