title: WPF ListView not rendering properly
body: <p>I have created a ListView control to display users who are connected to my application. It was working properly before I added the CellTemplates to the GridViewColumns. Now when I add an item to the list, it does not show up. However, if I close and reopen the list the item will appear. So it seems the item is being added properly, but the control is not rendering after the item is added. Code is below. Thanks in advance for any assistance.</p>
  
  <pre><code>public class ncWindow
  {   
      public Grid _Grid;
      public ListView lstConnectedUsers;          
      private GridView connectedUsersGrid;
  
      public List&lt;ConnectedUserItem&gt; roomUsers = new List&lt;ConnectedUserItem&gt;();
  
      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) =&gt; contextMenu.OnOpen();                
  
              ConnectedUserItem i = 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 i = 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
      {
          get { return imgPath; }
          set { imgPath = value; OnPropertyChanged("ImgPath"); }
      }
  
      public string UserName
      {
          get { return userName; }
          set { userName = value; OnPropertyChanged("UserName"); }
      }
  
      public int UserID
      {
          get { return userID; }
          set { userID = value; OnPropertyChanged("UserID"); }
      }
  
      public UserContextMenu Menu
      {
          get {  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)
      {                   
  
      }
  }
  </code></pre>