我有一個可觀察的集合正在從網絡線程填充。如果使用虛擬數據從構造函數填充OC,則會在UI上顯示。我知道這些項目正在從網絡線程添加到集合中,但項目數量從未在UI上更新。ObservableCollection不更新項目源
我的視圖模型如下:
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
我模特兒集合結合下面的視圖。
<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>
我試過包裝和包裝整個功能,它仍然不顯示在GUI上。我知道這通常有效,但在這種情況下,我沒有這樣做的運氣。 – joshwl2003