alielie
2026-04-23 ef52fd6fd07508883dce4eb002acda59f0169e59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using HMI.Base;
using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
 
namespace Plugin.TsExecution
{
    public class ColorConverter : IMultiValueConverter
    {
        public static ColorConverter Instance = new ColorConverter();
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0] is ObservableCollection<string> list && values[1] is string tag)
            {
                if (list?.Contains(tag) == true) return Brushes.Green;
                return Brushes.White;
            }
            return values;
        }
 
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
 
    /// <summary>
    /// ScreenTouch.xaml 的交互逻辑
    /// </summary>
    public partial class ScreenTouch : NotifyControl
    {
        public static readonly DependencyProperty ValuesProperty = DependencyProperty.Register(
            nameof(Values),
            typeof(ObservableCollection<bool>),
            typeof(ScreenTouch),
            new PropertyMetadata(null, OnValuesChanged));
 
        private static void OnValuesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is ScreenTouch control)
            {
                // 如果有旧的集合,取消订阅
                if (e.OldValue is ObservableCollection<bool> oldCollection)
                {
                    oldCollection.CollectionChanged -= control.Value_CollectionChanged;
                }
 
                // 如果有新的集合,订阅变更事件
                if (e.NewValue is ObservableCollection<bool> newCollection)
                {
                    newCollection.CollectionChanged += control.Value_CollectionChanged;
                }
 
                // 刷新所有单元格
                control.PaintPoints();
            }
        }
 
 
        public ScreenTouch(int rows, int columns)
        {
            this.rows = rows;
            this.columns = columns;
            InitializeComponent();
            Refresh();
            Values = new ObservableCollection<bool>(new bool[rows * columns]);
        }
 
        public ObservableCollection<bool> Values
        {
            get => (ObservableCollection<bool>)GetValue(ValuesProperty);
            set
            {
                SetValue(ValuesProperty, value);
            }
        }
 
        private void Value_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            PaintPoints();
        }
 
        public int rows { get; private set; }
        public int columns { get; private set; }
 
        private Border[] children;
 
        public void Refresh()
        {
            uniform.Children.Clear();
            uniform.Rows = rows;
            uniform.Columns = columns;
 
            children = new Border[rows * columns];
 
 
            for (int i = 0; i < children.Length; i++)
            {
                var border = new Border
                {
                    BorderThickness = new Thickness(0),
                    Background = Brushes.White
                };
                children[i] = border;
                uniform.Children.Add(border);
            }
        }
 
        public void Refresh(int rows, int columns)
        {
            this.rows = rows;
            this.columns = columns;
            Refresh();
        }
 
        public void Clear()
        {
            Values = new ObservableCollection<bool>(new bool[rows * columns]);
        }
 
        public void DrawPoint(int x, int y)
        {
            Values[y * columns + x] = true;
        }
 
        public void DrawPoints(int[][] points)
        {
            var temp = new ObservableCollection<bool>(new bool[rows * columns]);
            for (int i = 0; i < points.Length; i++)
            {
                int[] point = points[i];
                int index = point[1] * columns + point[0];
                //children[point[0]][point[1]].Background = Brushes.Green;
                temp[index] = true;
            }
            Values = temp;
        }
 
        private void PaintPoints()
        {
            for (int i = 0; i < Values.Count; i++)
            {
                if (Values[i])
                {
                    children[i].Background = Brushes.Green;
                }
                else
                {
                    children[i].Background = Brushes.White;
                }
            }
        }
    }
}