WPF ListView未正确呈现
本文关键字:ListView WPF | 更新日期: 2023-09-27 18:29:27
我创建了一个ListView控件来显示连接到我的应用程序的用户。在我将CellTemplates添加到GridViewColumns之前,它工作正常。现在,当我将项目添加到列表中时,它不会显示。但是,如果我关闭并重新打开该列表,则会显示该项目。因此,该项似乎被正确添加,但控件在添加该项后没有呈现。代码如下。提前感谢您的帮助。
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 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)
{
}
}
使用
private ObservableCollection<ConnectedUserItem> roomUsers = new ObservableCollection<ConnectedUserItem>();
而不是
public List<ConnectedUserItem> roomUsers = new List<ConnectedUserItem>();
如果仍然不起作用,您可以在向roomUsers
插入新项目后添加这些行来强制刷新
var view = CollectionViewSource.GetDefaultView(ItemsSource);
view.Refresh();