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
the reason why listviewtest.containerfromitem(item)
returning null because
- container not rendered yet
- no item found
- 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
Post a Comment