DataSelectControl.xaml.cs
4.3 KB
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
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;
}
}
}
}