DataSelectControl.xaml.cs 4.3 KB
using MemoryRead;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace SmartScan.SetControl.WPF
{
    public partial class DataSelectControl : Window
    {
        private readonly List<Dictionary<string, string>> _dataList;
        private int _selectedIndex = -1;
        private string dataKey = "";
        public Dictionary<string, string> SelectedData { get; private set; }

        public DataSelectControl(string datakey, List<Dictionary<string, string>> dataList)
        {
            this.dataKey = datakey;
            InitializeComponent();
            Topmost = true;
            _dataList = dataList ?? new List<Dictionary<string, string>>();
            MsgCenter.Text = string.Format(LanguageWwitchover.Dialog("SelectDatasetMessage", "{0} 匹配到多行数据,请选择:"), datakey);
            BtnOK.Content = LanguageWwitchover.Dialog("WPF_Ok", "确定");
            LoadLabels();
            if (_dataList.Count > 0 && Container.Children.Count > 0 && Container.Children[0] is Label first)
                SelectLabel(first, 0);
        }

        private void LoadLabels()
        {
            Container.Children.Clear();
            if (_dataList.Count <= 2)
            {
                Container.ItemWidth = 800;
            }
            else
            {
                Container.ItemWidth = 380;
            }
            for (int i = 0; i < _dataList.Count; i++)
            {
                var label = CreateItemLabel(i, _dataList[i]);
                Container.Children.Add(label);
            }
        }

        private Label CreateItemLabel(int index, Dictionary<string, string> data)
        {
            var tb = new System.Windows.Controls.TextBlock
            {
                TextWrapping = TextWrapping.Wrap,
                Text = BuildDatasetText(data),
                Margin = new Thickness(4),
                Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(234, 234, 234)),
                FontSize = 14
            };
            var lbl = new Label
            {
                Content = tb,
                Padding = new Thickness(10),
                Margin = new Thickness(8),
                BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(102, 102, 102)),
                BorderThickness = new Thickness(1),
                Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(48, 48, 48)),
                Cursor = System.Windows.Input.Cursors.Hand,
                Tag = index,
                Width = 380,
            };
            lbl.MouseLeftButtonUp += (s, e) => SelectLabel((Label)s, (int)lbl.Tag);
            return lbl;
        }

        private void SelectLabel(Label lbl, int index)
        {
            _selectedIndex = index;
            foreach (var child in Container.Children)
            {
                if (child is Label l)
                {
                    l.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(48, 48, 48));
                    l.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(102, 102, 102));
                }
            }
            lbl.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 120, 215));
            lbl.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 120, 215));
        }

        private string BuildDatasetText(Dictionary<string, string> data)
        {
            var parts = new List<string>();
            foreach (var kv in data)
            {
                parts.Add($"{kv.Key}: {kv.Value}");
            }
            return string.Join("\n", parts);
        }

        private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            if (_selectedIndex >= 0 && _selectedIndex < _dataList.Count)
            {
                SelectedData = _dataList[_selectedIndex];
                LogUtil.info("用户选择了数据集: " + dataKey + ", " + BuildDatasetText(SelectedData));
                DialogResult = true;
            }
            else
            {
                DialogResult = false;
            }
        }

    }
}