我与WPF用户界面的聊天应用程序(MVVM模式),我可以显示聊天列表静态和聊天消息静态在代码中,但我不能同步它时,从数据库中获得新数据,应用程序与单例
#region Singleton
/// <summary>
/// A single instance of the design model
/// </summary>
///
public static ChatListDesignModel Instance => new ChatListDesignModel();
public ChatListDesignModel()
{
Items = new List<ChatListItemViewModel>
{
new ChatListItemViewModel
{
Name = "Luke1111111",
Initials = "LM",
Message = "This chat app is awesome! I bet it will be fast too",
ProfilePictureRGB = "3099c5", //fe4503 00d405 3099c5
NewContentAvailable = true,
IsSelected = true
},
};
}此项目已清除,但当我将新项目添加到项目中时,它不会插入到UI中
<Grid DataContext="{x:Static core:ChatListDesignModel.Instance}" Background="{StaticResource ForegroundLightBrush}">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding Items }">聊天消息定义为ObservableCollection,并且也不会重新刷新或从实时数据库获取新值
发布于 2017-11-19 17:19:58
视图是不会获得关于List内部更改的通知。相反,您应该使用ObservableCollection。The ObservableCollection
表示一个动态数据集合,它在添加、删除项或刷新整个列表时提供通知。
这意味着您的视图可以识别所需的更改并更新GUI。
只需将项目更改为ObservableCollection<ChatListItemViewModel>即可。您还可以像这样初始化它
Items = new ObservableCollection<ChatListItemViewModel>
{
...https://stackoverflow.com/questions/47375227
复制相似问题