gpt4 book ai didi

c# - 如何使用 MVVM 将 ListView 数据与 List 绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 10:36:02 27 4
gpt4 key购买 nike

我正在尝试将 List<> 绑定(bind)到 ListView。当我更新时,我将清除列表。清除 ObservableCollection 有点慢。
问题是 View 中的东西没有正确更新。

XAML

<StackPanel.Resources>
<ResourceDictionary>
<common:BoolToBackgroundConverter x:Key="BoolToBackground"/>
<tb:StringInlineCollectionConvertor x:Key="InlineConvert"/>
</ResourceDictionary>
</StackPanel.Resources>
<ListView ItemsSource="{Binding NotificationsCollection, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
ScrollViewer.CanContentScroll="False">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Border>
<Grid Background="{Binding NotSeen,Converter={StaticResource BoolToBackground},UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Source="{Binding Thumb}"/>
<tb:BindableTextBlock InlineCollection="{Binding Text, Converter={StaticResource InlineConvert}}"/>
<TextBlock Text="{Binding Created}"/>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

C#
View 模型
public List<NotificationDataModel> notificationsCollection;
public List<NotificationDataModel> NotificationsCollection
{
get
{
if (notificationsCollection == null)
{
notificationsCollection = new List<NotificationDataModel>();
}
return notificationsCollection;
}
set
{
if (notificationsCollection == null)
{
notificationsCollection = new List<NotificationDataModel>();
}
notificationsCollection.Clear();
foreach (var item in value)
{
notificationsCollection.Add(item);
}
this.OnPropertyChanged("NotificationsCollection");
}
}

public void UpdateNotifications1()
{
List<NotificationDataModel> newCollection = new List<NotificationDataModel>();
newCollection.Add(item1);
newCollection.Add(item2);
newCollection.Add(item3);
newCollection.Add(item4);
newCollection.Add(item5);
newCollection.Add(item6);
this.NotificationsCollection = newCollection;
}

public void UpdateNotifications2()
{
List<NotificationDataModel> newCollection = new List<NotificationDataModel>();
newCollection.Add(item1);
newCollection.Add(item2);
newCollection.Add(item6);
this.NotificationsCollection = newCollection;
}

当我调用 UpdateNotifications1 元素会相应地显示,但之后当我调用 UpdateNotifications2 时,我会看到 item1、item2 和 item3 而不是 item6。
此外,在关闭 View 并重新打开这些项目后,项目正在为 NotSeen(例如黑色,初始为白色)属性获得新值,但它们仍然具有黑色背景。

最佳答案

问题是提高 PropertyChanged您的 NotificationsCollection 中的事件当底层字段的实际值没有改变时,setter 是无效的。目标ItemsSource属性接收相同的 List 实例(来自绑定(bind)),因此不会触发 UI 更新。

因此,不要清除并复制到现有集合,而是使用传递给 setter 的集合:

public List<NotificationDataModel> NotificationsCollection
{
get { return notificationsCollection; }
set
{
notificationsCollection = value;
OnPropertyChanged("NotificationsCollection");
}
}

关于c# - 如何使用 MVVM 将 ListView 数据与 List 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29032274/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com