C# ListView的扩展模型
我使用实体框架生成我的模型。它包含一些静态数据。我将其扩展如下:C# ListView的扩展模型,c#,wpf,entity-framework,listview,C#,Wpf,Entity Framework,Listview,我使用实体框架生成我的模型。它包含一些静态数据。我将其扩展如下: public partial class automaty { public bool ConnectionAvailable { get; set; } } 字段ConnectionAvailable确定设备是否可ping 在主窗口中,我有名为ListView的ListView和方法刷新: void Refresh() { new Thread(delegate() {
public partial class automaty
{
public bool ConnectionAvailable { get; set; }
}
字段ConnectionAvailable确定设备是否可ping
在主窗口中,我有名为ListView的ListView和方法刷新:
void Refresh()
{
new Thread(delegate()
{
this.Dispatcher.Invoke((Action)(() =>
{
var automats = Controller.Automats;
listView.ItemsSource = automats;
listView.UpdateLayout();
}));
}).Start();
}
每30秒调用一次。它工作得很好,但我也想显示设备状态。因此,我创建了无限循环线程,用于ping每个设备:
void PingLoop()
{
while (isInPingLoop)
{
var automats = listView.Items.Cast<automaty>().ToList();
foreach (var automat in automats)
{
if (isInPingLoop == false)
{
return;
}
var connection = Tools.HasConnection(automat.Adres_IP);
automat.ConnectionAvailable = connection;
}
}
}
鉴于此,它是绑定的:
<Image Source="{Binding ConnectionAvailable, Converter={StaticResource BoolToYesNoConverter}, ConverterParameter='pack://application:,,,/MachinesManager;component/Images/green-circle.png|pack://application:,,,/MachinesManager;component/Images/red-circle.png'}">
我的问题是,地位圈并不令人耳目一新。我知道PingLoop可以ping时,会将ConnectionAvailable字段设置为true,但它不会显示在屏幕上。PingLoop在哪个线程中运行?它在UI线程上吗?如果是这样,您的绑定更改将永远不会传播到UI视图。我通过添加包含机器状态的新bool数组来管理它,在刷新listview源之后,我只是将这些值分配给对象。