欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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));
        }


    }


}

 

相关标签: C#知识点