gpt4 book ai didi

c# - ObservableCollection 不更新项目源

转载 作者:太空宇宙 更新时间:2023-11-03 21:37:24 26 4
gpt4 key购买 nike

我有一个从网络线程填充的可观察集合。如果使用虚拟数据从构造函数填充 OC,它会显示在 UI 上。我知道项目正在从网络线程添加到集合中,但项目计数永远不会在 UI 上更新。

我的 View 模型如下:

public class ManikinStatusViewModel : DiViewModelBase
{
private readonly ICommunicationService manikinCommunicationService;

#region Properties
public ObservableCollection<CasualtyStatusViewModel> Manikins { get; private set; }
#endregion

public ManikinStatusViewModel()
{
Manikins = new ObservableCollection<CasualtyStatusViewModel>();

uow = UnitOfWorkFactory.Instance.CreateRunScenarioUnitOfWork(false);

AccelermeterPacketData apd = new AccelermeterPacketData(56, 57, 58);

manikinCommunicationService = new TestingCommunicationService(LoadAnalogSensorData(), apd);
manikinCommunicationService.ManikinDataReceived += ManikinCommunicationService_ManikinDataReceived;

}

#region Events
private void ManikinCommunicationService_ManikinDataReceived(object sender, ManikinDataReceivedEventArgs e)
{
if (e.ManiknDataPacket != null)
{
var manikin = Manikins.ToList().Find(m => m.ManikinId == e.ManiknDataPacket.SerialNumber);

if (manikin == null)
{
Debug.WriteLine("Creating manikin with serial number: " + e.ManiknDataPacket.SerialNumber);

CasualtyStatusViewModel csvm = new CasualtyStatusViewModel(e.ManiknDataPacket.SerialNumber);
Manikins.Add(csvm);

manikin = csvm;

Debug.WriteLine("manikin count is: " + Manikins.Count());

}

manikin.UpdateManikinStatus(e.ManiknDataPacket);
}
}

#endregion

我将人体模型集合绑定(bind)到以下 View 。

   <TabControl Grid.Row="1" ItemsSource="{Binding Manikins}" Name="ManikinsTabControl">
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Header" Value="Casualty"></Setter>
<Setter Property="ContentTemplate" Value="{StaticResource CasualtyTemplate}"></Setter>
</Style>
</TabControl.Resources>
</TabControl>

最佳答案

您必须在 View 模型中更新集合时引发 PropertyChanged 事件并且您的 View 模型必须实现 INotifyPropertyChanged

关于c# - ObservableCollection 不更新项目源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21049713/

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