WPF DataGrid使用 [Code]
程序员文章站
2022-07-13 22:39:47
...
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;
using System.ComponentModel;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Collections.Generic;
using System;
using System.Timers;
using System.Reflection;
using System.Collections.ObjectModel;
namespace GloveBoxInsp
{
/// <summary>
/// IoPortViewer.xaml 的交互逻辑
/// </summary>
public partial class IoPortViewer : WindowX
{
ObservableCollection<IoStatus> _lsInportStatus = new ObservableCollection<IoStatus>();
ObservableCollection<IoStatus> _lsOutportStatus = new ObservableCollection<IoStatus>();
//public IList<IoStatus> _lsInportStatus { get; set; }
//public IList<IoStatus> _lsOutportStatus { get; set; }
// Fields
private bool intialized;
private PortMap portMap;
private DigitalIoHandler digitalIoHandler;
private bool[] outputValueChecker;
private IContainer components;
private Timer timer;
public IoPortViewer(DigitalIoHandler digitalIoHandler, PortMap portMap)
{
this.InitializeComponent();
((this.FindName("inportDataGrid")) as DataGrid).ItemsSource = _lsInportStatus;
((this.FindName("outportDataGrid")) as DataGrid).ItemsSource = _lsOutportStatus;
this.digitalIoHandler = digitalIoHandler;
this.portMap = portMap;
int num = 1;
foreach (DigitalIo io in digitalIoHandler)
{
DigitalIoType digitalIoType = io.DigitalIoType;
this.comboBoxDigitalIo.Items.Add($"{num} : {digitalIoType.ToString()}");
num++;
}
this.comboBoxDigitalIo.SelectedIndex = 0;
timer = new Timer();
timer.Enabled = true;
timer.Elapsed += Timer_Elapsed;
timer.Interval = 100;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (base.IsVisible)
{
this.UpdatePortTable();
}
}
private void WindowX_Loaded(object sender, RoutedEventArgs e)
{
this.InitPortTable();
this.timer.Start();
}
private void InitPortTable()
{
intialized = false;
int selectedIndex = this.comboBoxDigitalIo.SelectedIndex;
DigitalIo io = this.digitalIoHandler.Get(selectedIndex);
if (io != null)
{
int numInPort = io.NumInPort;
int num3 = 0;
foreach (string str in this.portMap.inPort.GetPortNames(selectedIndex, numInPort))
{
this._lsInportStatus.Add(new IoStatus( num3,str,false ));
num3++;
}
int numOutPort = io.NumOutPort;
num3 = 0;
foreach (string str2 in this.portMap.outPort.GetPortNames(selectedIndex, numInPort))
{
IoStatus a = new IoStatus(num3, str2, false);
a.PropertyChanged += A_PropertyChanged;
this._lsOutportStatus.Add(a);
num3++;
}
this.outputValueChecker = new bool[num3];
}
intialized = true;
}
private void A_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (intialized)
{
IoStatus ioStatus = sender as IoStatus;
if (ioStatus != null)
{
if (ioStatus.Name == "") return;
IoPort ioPort = new IoPort(ioStatus.Name, -1);
int deIndex = 0;
Dispatcher.Invoke(new Action(delegate { deIndex = this.comboBoxDigitalIo.SelectedIndex; }));
//var tm = _lsOutportStatus.ToList<IoStatus>();
ioPort.Set(ioStatus.No, deIndex);
LogHelper.Debug(LoggerType.IO, $"Write Output : {ioStatus.No} -> {ioStatus.Status.ToString()}");
this.digitalIoHandler.WriteOutput(ioPort, ioStatus.Status);
}
}
}
private void outportTable_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
int index = outportDataGrid.SelectedIndex;
if (_lsOutportStatus[index].Name == "") return;
IoPort ioPort = new IoPort(_lsOutportStatus[index].Name, -1);
ioPort.Set(index, this.comboBoxDigitalIo.SelectedIndex);
LogHelper.Debug(LoggerType.IO, $"Write Output : {index} -> {_lsOutportStatus[index].Status.ToString()}");
this.digitalIoHandler.WriteOutput(ioPort, _lsOutportStatus[index].Status);
}
//private void timer_Tick(object sender, EventArgs e)
//{
// if (base.IsVisible)
// {
// this.UpdatePortTable();
// }
//}
private void UpdatePortTable()
{
int selectedIndex = 0;
Dispatcher.Invoke(new Action(delegate { selectedIndex = this.comboBoxDigitalIo.SelectedIndex; }));
DigitalIo io = this.digitalIoHandler.Get(selectedIndex);
if (io != null)
{
int numInPort = io.NumInPort;
uint num2 = this.digitalIoHandler.ReadInputGroup(selectedIndex, 0);
for (int i = 0; i < this._lsInportStatus.Count; i++)
{
this._lsInportStatus[i].Status = (((num2 >> (i & 0x1f)) & 1) != 1) ? false : true;
}
int num6 = io.NumInPort;
uint num3 = this.digitalIoHandler.ReadOutputGroup(selectedIndex, 0);
for (int j = 0; j < this._lsOutportStatus.Count; j++)
{
bool flag = (num3 >> j & 1u) == 1u;
this.outputValueChecker[j] = flag;
this._lsOutportStatus[j].Status = !flag ? false : true;
}
}
}
private void WindowX_Closing(object sender, CancelEventArgs e)
{
//if (ExtentionClass.IsModal(this))
//{
// e.Cancel = true;
// base.Hide();
//}
timer.Stop();
timer.Dispose();
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
timer.Stop();
timer.Dispose();
//if (ExtentionClass.IsModal(this))
//{
// this.Hide();
//}
//else { this.Close(); }
}
private void comboBoxDigitalIo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.intialized)
{
this.InitPortTable();
}
}
private void WindowX_Closed(object sender, EventArgs e)
{
if (this.components != null)
{
this.components.Dispose();
}
}
}
public static class ExtentionClass
{
public static bool IsModal(this Window window)
{
var filedInfo = typeof(Window).GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic);
return filedInfo != null && (bool)filedInfo.GetValue(window);
}
}
public class IoStatus: INotifyPropertyChanged
{
private int _no;
private string _name;
private bool _status;
[DisplayName("端口号")]
[ReadOnlyColumn]
[ColumnWidth("1*")]
public int No {
get
{
return _no;
}
set
{
_no = value;
OnPropertyChanged("No");
}
}
[DisplayName("名称")]
[ReadOnlyColumn]
[ColumnWidth("3*")]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
OnPropertyChanged("Name");
}
}
[DisplayName("状态")]
[ColumnWidth("1*")]
public bool Status
{
get
{
return _status;
}
set
{
_status = value;
OnPropertyChanged("Status");
}
}
public IoStatus(int no, string name, bool s)
{
No = no; Name = name ; Status = s;
}
public event PropertyChangedEventHandler PropertyChanged;
public override string ToString() => $"{No}:{No} {Name} {Status}";
protected void OnPropertyChanged(string info)
{
var handler = PropertyChanged;
if (handler!= null)
{
handler.Invoke(this, new PropertyChangedEventArgs(info));
}
//handler?.Invoke(this, new PropertyChangedEventArgs(info));
}
}
}
上一篇: CSocket如何通知窗口(MFC)
下一篇: WPF 窗口/页 的位置
推荐阅读
-
WPF默认控件模板的获取和资源词典的使用
-
visual studio code教程 vscode的基础使用和自定义设置方法
-
详解使用DotNet CLI创建自定义的WPF项目模板
-
VisualStudio Code怎么使用全局搜索快捷键?
-
决定何时使用 DataGrid、DataList 或 Repeater(ASP.NET 技术文章)
-
WPF图片,DataGrid等实现圆角
-
WPF通过线程使用ProcessBar的方法详解
-
WPF DataGrid显示MySQL查询信息,且可删除、修改、插入 (原发布 csdn 2018-10-13 20:07:28)
-
详解VS Code使用之Vue工程配置format代码格式化
-
CKEditor 4.4.1 添加代码高亮显示插件功能教程【使用官方推荐Code Snippet插件】