c# - WPF ListView not rendering properly -
i have created listview control display users connected application. working before added celltemplates gridviewcolumns. when add item list, not show up. however, if close , reopen list item appear. seems item being added properly, control not rendering after item added. code below. in advance assistance.
public class ncwindow { public grid _grid; public listview lstconnectedusers; private gridview connectedusersgrid; public list<connecteduseritem> roomusers = new list<connecteduseritem>(); public ncwindow(grid grid) { _grid=grid; setter setter = new setter(); setter.property = gridviewcolumnheader.visibilityproperty; setter.value = visibility.collapsed; style style = new style(); style.targettype = typeof(gridviewcolumnheader); style.setters.add(setter); style lcustyle = new style(typeof(listviewitem)); trigger lcutrigger = new trigger(); lcutrigger.property = listviewitem.isselectedproperty; lcutrigger.value = true; setter lcusetter = new setter(listviewitem.foregroundproperty,brushes.blue); frameworkelementfactory textblockfactory = new frameworkelementfactory(typeof(textblock)); textblockfactory.setbinding(textblock.textproperty,new binding("username")); textblockfactory.setbinding(textblock.contextmenuproperty,new binding("menu")); datatemplate textblockdatatemplate = new datatemplate(); textblockdatatemplate.visualtree = textblockfactory; frameworkelementfactory imagefactory = new frameworkelementfactory(typeof(image)); imagefactory.setbinding(image.sourceproperty,new binding("imgpath")); datatemplate imgdatatemplate = new datatemplate(); imgdatatemplate.visualtree = imagefactory; gridviewcolumn statuscolumn = new gridviewcolumn(); statuscolumn.width = 10; statuscolumn.celltemplate = imgdatatemplate; gridviewcolumn usercolumn = new gridviewcolumn(); usercolumn.width = 100; usercolumn.celltemplate = textblockdatatemplate; gridviewcolumn clmuserid = new gridviewcolumn(); clmuserid.displaymemberbinding = new binding("userid"); clmuserid.width = 0; connectedusersgrid = new gridview(); connectedusersgrid.columns.add(statuscolumn); connectedusersgrid.columns.add(usercolumn); connectedusersgrid.columns.add(clmuserid); connectedusersgrid.columnheadercontainerstyle = style; lstconnectedusers = nc.getnewlistview();//new listview(); lstconnectedusers.horizontalalignment = horizontalalignment.right; lstconnectedusers.margin = new thickness(0,2,2,34); lstconnectedusers.width = 130; lstconnectedusers.view = connectedusersgrid; lstconnectedusers.selectionmode = selectionmode.single; lstconnectedusers.itemcontainerstyle = lcustyle; lstconnectedusers.itemssource = roomusers; grid.children.add(lstconnectedusers); } public void addconnecteduser(clientaccess.svc.client user, clientaccess.svc.room room) { try { usercontextmenu contextmenu = null; contextmenu = new usercontextmenu(nc,user); contextmenu.opened += (sender,args) => contextmenu.onopen(); connecteduseritem = new connecteduseritem(){ imgpath = "", username = user.username, userid = user.userid, menu = contextmenu}; roomusers.insert(0,i); } catch(exception ex) { } } public void removeconnecteduser(clientaccess.svc.client user) { try { connecteduseritem = roomusers.find(delegate(connecteduseritem ii){return ii.userid==user.userid;}); roomusers.remove(i); } catch(exception ex) { } } } public class connecteduseritem : inotifypropertychanged { private string imgpath; private string username; private int userid; private usercontextmenu menu; public event propertychangedeventhandler propertychanged; public string imgpath { { return imgpath; } set { imgpath = value; onpropertychanged("imgpath"); } } public string username { { return username; } set { username = value; onpropertychanged("username"); } } public int userid { { return userid; } set { userid = value; onpropertychanged("userid"); } } public usercontextmenu menu { { return menu; } set { menu = value; } } protected void onpropertychanged(string propertyname) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(propertyname)); } } public class usercontextmenu : contextmenu { private clientaccess.svc.client client; private menuitem misilence, miaddbuddy, miprivatemessage; public usercontextmenu(clientaccess.svc.client _client) { client = _client; misilence = new menuitem(); misilence.header = "silence"; misilence.click += misilence_click; miaddbuddy = new menuitem(); miaddbuddy.header = "friends"; miaddbuddy.click += miaddbuddy_click; miprivatemessage = new menuitem(); miprivatemessage.header = "send message"; miprivatemessage.click += miprivatemessage_click; items.add(miprivatemessage); items.add(miaddbuddy); items.add(misilence); } public void onopen() { } private void misilence_click(object sender, routedeventargs e) { } private void miaddbuddy_click(object sender, routedeventargs e) { } private void miprivatemessage_click(object sender, routedeventargs e) { } }
use
private observablecollection<connecteduseritem> roomusers = new observablecollection<connecteduseritem>();
instead of
public list<connecteduseritem> roomusers = new list<connecteduseritem>();
if still doesn't work, can force refresh adding these lines after insert new item roomusers
var view = collectionviewsource.getdefaultview(itemssource); view.refresh();
Comments
Post a Comment