schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using XHandler.Class;
 
namespace XHandler.Controls
{
    /// <summary>
    /// 板位类型耗材
    /// </summary>
    public class ShelfLabware : Grid
    {
        //private int nBaseWidth = 16;
        //private int nBaseHeight = 9;
        /// <summary>
        /// 耗材的宽(真实比例换算之后的像素宽度)
        /// </summary>
        public double ShelfWidth
        {
            get { return (double)GetValue(ShelfWidthProperty); }
            set { SetValue(ShelfWidthProperty, value); }
        }
        public static readonly DependencyProperty ShelfWidthProperty =
            DependencyProperty.Register("ShelfWidth", typeof(double), typeof(ShelfLabware), new PropertyMetadata(Double.NaN));
 
        /// <summary>
        /// 耗材的长(真实比例换算之后的像素宽度)
        /// </summary>
        public double ShelfLength
        {
            get { return (double)GetValue(ShelfLengthProperty); }
            set { SetValue(ShelfLengthProperty, value); }
        }
        public static readonly DependencyProperty ShelfLengthProperty =
            DependencyProperty.Register("ShelfLength", typeof(double), typeof(ShelfLabware), new PropertyMetadata(Double.NaN));
 
        /// <summary>
        /// 板位的名称
        /// </summary>
        public string ShelfName
        {
            get { return (string)GetValue(ShelfNameProperty); }
            set { SetValue(ShelfNameProperty, value); }
        }
        public static readonly DependencyProperty ShelfNameProperty =
            DependencyProperty.Register("ShelfName", typeof(string), typeof(ShelfLabware), new PropertyMetadata("", OnLabelChanged));
 
        /// <summary>
        /// 板位是垃圾桶
        /// </summary>
        public bool ShelfTrash
        {
            get { return (bool)GetValue(ShelfTrashProperty); }
            set { SetValue(ShelfTrashProperty, value); }
        }
        public static readonly DependencyProperty ShelfTrashProperty =
            DependencyProperty.Register("ShelfTrash", typeof(bool), typeof(ShelfLabware), new PropertyMetadata(false, OnShelfTrashChanged));
 
        /// <summary>
        /// 板位的数据库Id
        /// </summary>
        public string ShelfDBId
        {
            get { return (string)GetValue(ShelfDBIdProperty); }
            set { SetValue(ShelfDBIdProperty, value); }
        }
        public static readonly DependencyProperty ShelfDBIdProperty =
            DependencyProperty.Register("ShelfDBId", typeof(string), typeof(ShelfLabware), new PropertyMetadata(""));
 
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }
 
        // 依赖属性
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register(
                "IsSelected", typeof(bool), typeof(ShelfLabware),
                new PropertyMetadata(false, OnIsSelectedChanged));
 
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            // 处理左键抬起的逻辑
        }
 
        private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShelfLabware control = (ShelfLabware)d;
            if (control != null)
            {
                // 当IsSelected属性改变时,这里可以添加更多的逻辑处理
                if ((bool)e.NewValue)
                {
                    // 设置控件被选中时的样式
                    //control.Background = Brushes.Blue;
                    RectangleGeometry rectangleGeometry = new RectangleGeometry(new Rect(0, 0, 70, 70), 0, 0);
                    GeometryDrawing MaskDrawing = new GeometryDrawing(Brushes.White, null, rectangleGeometry);
                    DrawingBrush drawingBrush = new DrawingBrush(MaskDrawing);
                    control.OpacityMask = drawingBrush;
                    drawingBrush.Stretch = Stretch.Fill;
                    control.OpacityMask.Opacity = 0.5d;
                    //Rectangle rtl = new Rectangle();
                    //rtl.StrokeDashArray = new DoubleCollection { 4,2};
                    //rtl.Stroke = Brushes.Gray;
                    //rtl.StrokeThickness =   1d;
                    //GeometryDrawing MaskDrawing = new GeometryDrawing(Brushes.White, null, rtl.Geo);
                    //DrawingBrush drawingBrush = new DrawingBrush(MaskDrawing);
                    //control.OpacityMask = drawingBrush;
                    //control.OpacityMask.Opacity = 0.5d;
                }
                else
                { 
                    // 设置控件未被选中时的样式
                    //control.Background = Brushes.Transparent;
                    control.OpacityMask.Opacity = 1d;
                }
            }
        }
 
        public static void OnLabelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShelfLabware control = (ShelfLabware)d;
            if (control != null)
            {
                string label = e.NewValue as string;
 
                control.SetLabel(label);
            }
        }
 
        #region 改变为垃圾桶标志
        public static void OnShelfTrashChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShelfLabware control = (ShelfLabware)d;
            if (control != null)
            {
                bool isTrash = Convert.ToBoolean(e.NewValue);
                if (isTrash)
                {
                    if (borderTrash != null)
                    {
                        control.Children.Remove(borderTrash);
                    }
                    control.DrawTrash();
                }
                else
                {
                    if (borderTrash != null)
                    {
                        control.Children.Remove(borderTrash);
                    }
                }
            }
        }
        #endregion
 
        static Border borderTrash;
        private void DrawTrash()
        {
            TextBlock tb = new TextBlock();
            tb.Text = Properties.Resources.strTrash;
            tb.FontSize = 10;
            tb.FontWeight= FontWeights.Bold;
            tb.Foreground = Brushes.Black;
            tb.VerticalAlignment = VerticalAlignment.Center;
            tb.HorizontalAlignment = HorizontalAlignment.Left;
 
            borderTrash = new Border()
            {
                Name = "Trash",
                Background = Brushes.DarkGray,
                CornerRadius = new CornerRadius(5),
                Child = tb,
            };
            //borderTrash.Loaded += BorderTrash_Loaded;
            this.Children.Add(borderTrash);
            Panel.SetZIndex(borderTrash, 4);
            Grid.SetColumn(borderTrash, 0);
            Grid.SetRow(borderTrash, 0);
        }
 
        private void BorderTrash_Loaded(object sender, RoutedEventArgs e)
        {
            Border tb = sender as Border;
            if (tb != null)
            {
                tb.VerticalAlignment = VerticalAlignment.Center;
                tb.HorizontalAlignment = HorizontalAlignment.Left;
            }
        }
 
        public void SetLabel(string text)
        {
            if (borderLabel == null)
                return;
            TextBlock tb = Utilities.FindVisualChild<TextBlock>(borderLabel);
            if (tb != null)
            {
                tb.Text = text;
            }
            if (string.IsNullOrEmpty(text))
                borderLabel.Visibility = Visibility.Collapsed;
            else
            {
                borderLabel.Visibility = Visibility.Visible;
                ((ToolTip)(tb.ToolTip)).Content = new TextBlock()
                {
                    Text = ShelfName,
                };
            }
        }
 
 
        public ShelfLabware()
        {
            this.SnapsToDevicePixels = true;
            InitializeComponent();
        }
 
        void InitializeComponent()
        {
            tooltip = new ToolTip();
            tooltip.Placement = PlacementMode.Right;
            tooltip.PlacementRectangle = new Rect(0, 0, 0, 0);
            tooltip.HorizontalOffset = 30;
            tooltip.VerticalOffset = -20;
 
            tooltipLabware = new ToolTip();
            tooltipLabware.Placement = PlacementMode.Right;
            tooltipLabware.PlacementRectangle = new Rect(0, 0, 0, 0);
            tooltipLabware.HorizontalOffset = 30;
            tooltipLabware.VerticalOffset = -20;
            this.SizeChanged += ControlLabware_SizeChanged;
        }
 
        private void ControlLabware_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.ActualWidth == double.NaN || this.ActualWidth <= 0)
                return;
 
            this.Children.Clear();
 
            DrawContent();
 
            AddLabel();
            Width = ShelfLength;
            Height = ShelfWidth;
        }
 
        private void DrawContent()
        {
            Path path = new Path()
            {
                Stroke = Brushes.Gray,//FindResource("blueBrush") as SolidColorBrush,
                StrokeThickness = 1,
                Fill = Brushes.DarkGray//FindResource("lightBlueBrush") as SolidColorBrush,
            };
            string strData = GetPathData();
 
            path.Data = Geometry.Parse(strData);
            this.Children.Add(path);
        }
 
        private string GetPathData()
        {
            string ret = "";
            double W = ShelfLength;
            double H = ShelfWidth;
 
            ret = string.Format("M0,0 L{0},0 L{1},{2} L0,{3}Z", W, W, H, H);
 
            return ret;
        }
 
 
        
 
        private Border borderLabel;
        private ToolTip tooltip;//鼠标悬停至标签提示文本
        private ToolTip tooltipLabware;//鼠标悬停至耗材提示文本
        private void AddLabel()
        {
            if(string.IsNullOrEmpty(ShelfName))
            {
                return;
            }
 
            tooltip.Content = new TextBlock()
            {
                Text = ShelfName,
            };
 
 
            TextBlock tb = new TextBlock();
            tb.Text = ShelfName;
            tb.Margin = new Thickness(2);
            tb.Foreground = Brushes.White;
            tb.TextWrapping = TextWrapping.Wrap;
            tb.ToolTip = tooltip;
 
            tooltipLabware.Content = new TextBlock()
            {
                Text = ShelfName,
            };
            this.ToolTip = tooltipLabware;
 
            borderLabel = new Border()
            {
                Name = "borderLabel",
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Background = new SolidColorBrush(Color.FromArgb(180, 34, 106, 255)),
                Width = this.ShelfLength,
                Height = 20,
                Child = tb,
            };
            if (string.IsNullOrEmpty(ShelfName))
                borderLabel.Visibility = Visibility.Collapsed;
            else
                borderLabel.Visibility = Visibility.Visible;
 
            this.Children.Add(borderLabel);
            Panel.SetZIndex(borderLabel, 5);
            Grid.SetColumn(borderLabel, 0);
            //Grid.SetColumnSpan(borderLabel, 2);
            Grid.SetRow(borderLabel, 0);
            //Grid.SetRowSpan(borderLabel, 2);
        }
    }
}