使用计时器更新DataGrid中的单个列 [英] Update a single column in a DataGrid with a timer

查看:65
本文介绍了使用计时器更新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中的单个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆