using HMI.Base; using HMI.UI.DpUtil; 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.StandardTest { 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 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; } } /// /// ScreenTouch.xaml 的交互逻辑 /// public partial class ScreenTouch : NotifyControl { public static readonly DependencyProperty ValuesProperty = DependencyProperty.Register( nameof(Values), typeof(ObservableCollection), typeof(ScreenTouch), new PropertyMetadata(null, OnValuesChanged)); private static void OnValuesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is ScreenTouch control) { // 如果有旧的集合,取消订阅 if (e.OldValue is ObservableCollection oldCollection) { oldCollection.CollectionChanged -= control.Value_CollectionChanged; } // 如果有新的集合,订阅变更事件 if (e.NewValue is ObservableCollection 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(new bool[rows * columns]); } public ObservableCollection Values { get => (ObservableCollection)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); } UniformGridHelper.RefreshGridInnerBorder(uniform); } public void Refresh(int rows, int columns) { this.rows = rows; this.columns = columns; Refresh(); } public void Clear() { Values = new ObservableCollection(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(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; } public void DrawNewPoints(int[][] points) { var temp = Values == null ? new ObservableCollection(new bool[rows * columns]) : new ObservableCollection(Values); for (int i = 0; i < points.Length; i++) { int[] point = points[i]; if (point == null) { continue; } 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; } } } } }