gpt4 book ai didi

c# - MVVM C#WPF-更改observablecollection中的项目时UI不更新

转载 作者:行者123 更新时间:2023-12-03 10:32:43 24 4
gpt4 key购买 nike

我已经看了两天有关此问题的Stackoverflow帖子,我似乎无法理解为什么我的代码无法正常工作

更改ObservableCollection中的项目时,似乎无法在UI中更新数据网格。

我知道ObservableCollection如果其中的项目发生更改,则不会触发PropertyChanged事件。

似乎其他人已经成功地做到了这一点,方法是将INotifyPropertyChanged添加到模型中,并在更改属性时调用OnPropertyChanged。我已经实现了这一点,并检查了PropertyChanged事件是否已结束。

将新项目添加到集合时,UI会更新。

我意识到这可能是线程问题,但是我不太了解如何检查或修复它。

我对编程非常陌生,并试图将头转向MVVM。

有什么建议?

型号:

public class ModelObj : INotifyPropertyChanged
{
public string Name { get; set; }
public string IpAddress { get; set; }

private DateTime timer;

public DateTime Timer
{
get { return timer; }
set
{
timer = value;
OnPropertyChanged();
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

ViewModel:
public class MainViewModel : ViewModelBase
{
public ObservableCollection<ModelObj> ModelObjects { get; } = new ObservableCollection<ModelObj>();

private IUdpDataService _udpDataService;

public MainViewModel(IUdpDataService udpDataService)
{
_udpDataService = udpDataService;
}

public void StartUdpDataService()
{
_udpDataService.StartBroadCasting();
_udpDataService.ReceivedDataEvent += ParseReceivedData;
}

private void ParseReceivedData(string receivedData)
{
// This object contains all the information in the received data packet.
UdpPacket udpPacket = new UdpPacket(receivedData);

// This object only contains the object name, IpAddress and a time variable.
ModelObj modelObj = new ModelObj
{
Name = udpPacket.Name,
IpAddress = udpPacket.IpEthernet,
Timer = DateTime.Now,
};

App.Current.Dispatcher.Invoke((Action)delegate
{
UpdateList(modelObj);
});
}

private void UpdateList(ModelObj modelObj)
{
var testObj = ModelObjects.FirstOrDefault(x => x.Name == modelObj.Name);
if (testObj != null)
{
testObj = modelObj
}
else
{
ModelObjects.Add(modelObj);
testObj = modelObj;
}
}
}

查看:
public partial class MainWindow : Window
{
private MainViewModel _viewModel;

public MainWindow(MainViewModel viewModel)
{
InitializeComponent();
_viewModel = viewModel;
DataContext = _viewModel;
Loaded += MainWindow_Loaded;
}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
_viewModel.StartUdpDataService();
}

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
Settings.Default.Save();
base.OnClosing(e);
}
}

XAML:
     <DataGrid Grid.Row="0" ItemsSource="{Binding Path=ModelObjects}"
IsReadOnly="True"
Background="white"
RowHeaderWidth ="0"
AutoGenerateColumns="False">

<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="auto" MinWidth="150"/>
<DataGridTextColumn Header="IP address" Binding="{Binding IpAddress}" Width="*"/>
<DataGridTextColumn Header="Timer" Binding="{Binding Timer, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
</DataGrid.Columns>
</DataGrid>

最佳答案

您应该设置现有对象的Timer属性:

private void UpdateList(ModelObj modelObj)
{
var testObj = ModelObjects.FirstOrDefault(x => x.Name == modelObj.Name);
if (testObj != null)
{
testObj.Timer = modelObj.Timer
}
else
{
ModelObjects.Add(modelObj);
}
}

当前,您正在获取对现有对象的引用,然后将保存该引用的 testObj变量设置为对传递给 ModelObj方法的新 UpdateList对象的引用。这不会更新 Timer ModelObjects集合中对象的 the属性。

关于c# - MVVM C#WPF-更改observablecollection中的项目时UI不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49941534/

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