使用计时器更新DataGrid中的单个列

编程入门 行业动态 更新时间:2024-10-11 09:20:26
本文介绍了使用计时器更新DataGrid中的单个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我发现 此主题,解释了如何实现计时器来更新DataGrid的一列。简单地说,我不能让它适合我的情况。我想ping一台计算机,然后用图像(.png)更新该列。我觉得我太近了...

I found this thread that explains how to implement a timer to update one column of a DataGrid. Simply put, I can't make it work for my situation. I want to ping a computer and then update that column with an image (.png). I feel like I'm so close...

XAML

<local:StatusConverter x:Key="onlineConverter" /> ... <DataGrid.Columns> <DataGridTemplateColumn x:Name="Online" Header="Online" IsReadOnly="True" Width="45" > <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Image Height="20" Width="20" Source="{Binding Status, Converter={StaticResource onlineConverter}}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns>

背后代码

Code behind

public partial class MainWindow : Window { char[] splitchar = { ',' }; private Process update; public MainWindow() { InitializeComponent(); }

{

public class OnlineInd:INotifyPropertyChanged { private Timer timer; 公共事件PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName)); } public OnlineInd() { timer = new Timer(TimeSpan.FromSeconds(30).TotalMilliseconds); timer.Elapsed + = Timer_Elapsed; timer.AutoReset = true; timer.Enabled = true; } private void Timer_Elapsed(对象发送者,ElapsedEventArgs e) { Ping pinger = new Ping(); 尝试 { PingReply reply = pinger.Send(" device",1500); if(reply.Status == IPStatus.Success) { Status = true; NotifyPropertyChanged(" Status"); } 其他 {状态=假; NotifyPropertyChanged(" Status"); } } catch(PingException) {} } private Boolean _status = false; public Boolean状态 { get { return _status; } 设置 { _status = value; NotifyPropertyChanged(" Status"); } } } 公共类StatusConverter:IValueConverter {公共对象转换(对象值,类型targetType ,对象参数,CultureInfo文化) { if((bool)value == true) { return Properties.Resources.green_light; } 其他 {返回Properties.Resources.red_light; } } 公共对象ConvertBack(对象值,类型targetType,对象参数,CultureInfo文化) { throw new NotImplementedException(); } }

public class OnlineInd : INotifyPropertyChanged { private Timer timer; public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public OnlineInd() { timer = new Timer(TimeSpan.FromSeconds(30).TotalMilliseconds); timer.Elapsed += Timer_Elapsed; timer.AutoReset = true; timer.Enabled = true; } private void Timer_Elapsed(object sender, ElapsedEventArgs e) { Ping pinger = new Ping(); try { PingReply reply = pinger.Send("device", 1500); if (reply.Status == IPStatus.Success) { Status = true; NotifyPropertyChanged("Status"); } else { Status = false; NotifyPropertyChanged("Status"); } } catch (PingException) { } } private Boolean _status = false; public Boolean Status { get { return _status; } set { _status = value; NotifyPropertyChanged("Status"); } } } public class StatusConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if ((bool)value == true) { return Properties.Resources.green_light; } else { return Properties.Resources.red_light; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }

推荐答案

我注意到我收到了这个Binding错误。

I notice that I get this Binding error.

System.Windows.Data Error: 40 : BindingExpression path error: 'Status' property not found on 'object' ''String' (HashCode=9712865)'. BindingExpression:Path=Status; DataItem='String' (HashCode=9712865); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource')

更多推荐

使用计时器更新DataGrid中的单个列

本文发布于:2023-10-31 09:09:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!