前言
大概20天以前水了一篇文章,主要讲了我封装一个小控件的经历,今天QA那边提了一个小bug,我在修改bug的同时重新思考了这个需求,顺便重构了一下代码,算是对上次水文的补充,我也会把全部代码都放出来。
该控件的主要作用就是展示仓库货架物品信息,并提供位置选择功能。
对象建模
选仓库,展示货架,货架按货架编号排序,货架中显示出货品,货品按种子编号和坐标排序,该控件就是一个聚合,三个层次,货架列表控件,包含货架控件,货架控件包含货品控件。 实体:货架列表 货架 货品 值对象:坐标 颜色(我为了简单只用了字符串)货品属性
控件截图
全部代码
public delegate void ChooseGoodsEventHanler(ChooseShelfEvevtArgs args); /// <summary> /// 货架物品 /// </summary> public partial class ShelfGoods : UserControl { private readonly List<GoodsProperty> _goodsProperties = new List<GoodsProperty>(); private readonly Point3D _point3D; private string _goodsNo; public ShelfGoods(Point3D point) { InitializeComponent(); _point3D = point; } public IEnumerable<GoodsProperty> GoodsProperties { get { return _goodsProperties; } } public Point3D Point3D { get { return _point3D; } } public string GoodsNo { get { return _goodsNo; } } public ChooseGoodsEventHanler ChoosedGoodsEventHanler = null; public void AddProperty(GoodsProperty goodsProperty) { if (_goodsProperties.Any(s => s.IsEquals(goodsProperty))) return; _goodsProperties.Add(goodsProperty); } public void AddProperties(IEnumerable<GoodsProperty> goodsProperties) { _goodsProperties.AddRange(goodsProperties); } public void SetGoodsNo(string goodsNo) { _goodsNo = goodsNo; TextGoodsNo.Text = goodsNo; } public void SetTip() { foreach (GoodsProperty property in GoodsProperties) { string tip = string.Format("{0}:{1}", property.PropertyName, property.PropertyValue); var label = new Label {Content = tip}; ToolPanel.Children.Add(label); } } public void ChangeBgColor(string color) { var solidColorBrush = new SolidColorBrush {Color = GetColors(color)}; BoxGrid.Background = solidColorBrush; } private Color GetColors(string color) { switch (color) { case "红色": return Colors.Red; case "橙色": return Colors.Orange; case "黄色": return Colors.Yellow; case "绿色": return Colors.Green; case "蓝色": return Colors.Blue; case "浅蓝色": return Colors.LightBlue; case "黑色": return Colors.Black; case "灰色": return Colors.Gray; default: return Colors.White; } } public override string ToString() { if (string.IsNullOrEmpty(GoodsNo)) return string.Format("x={0},y={1},z={2}", Point3D.PointX, Point3D.PointY, Point3D.PointZ); return string.Format("物品编号:{0},x={1},y={2},z={3}", GoodsNo, Point3D.PointX, Point3D.PointY, Point3D.PointZ); } private void BoxGrid_OnMouseDown(object sender, MouseButtonEventArgs e) { if (ChoosedGoodsEventHanler != null) { ChoosedGoodsEventHanler(new ChooseShelfEvevtArgs {GoodsNo = GoodsNo, GoodsPoint3D = Point3D}); } } } public class ChooseShelfEvevtArgs : EventArgs { public Point3D GoodsPoint3D { get; set; } public string GoodsNo { get; set; } public bool IsOnlySaveWarehouse { get; set; } public decimal WarehouseId { get; set; } public string WareHouseName { get; set; } } /// <summary> /// 货架物品坐标 /// </summary> public class Point3D { public Point3D(int x, int y, int z) { PointX = x; PointY = y; PointZ = z; } public int PointX { get; private set; } public int PointY { get; private set; } public int PointZ { get; private set; } public bool IsEquals(Point3D point) { if (point == null) return false; return PointX == point.PointX && PointY == point.PointY && PointZ == point.PointZ; } }
货品控件
/// <summary> /// 货架 /// </summary> public partial class ShelfLine : UserControl { private readonly int _colCount; private readonly int _layIndex; private readonly int _rowIndex; private readonly WarehouseBLL _warehouseBll = new WarehouseBLL(); private List<ShelfGoods> _goodses = new List<ShelfGoods>(); public ChooseGoodsEventHanler ChoosedGoodsEventHanler = null; public int ColCount { get { return _colCount; } } public int LayIndex { get { return _layIndex; } } public int RowIndex { get { return _rowIndex; } } public ShelfLine(int rowIndex, int colCount, int layIndex = 0) { InitializeComponent(); _rowIndex = rowIndex; _colCount = colCount; _layIndex = layIndex; for (int i = 0; i < colCount; i++) { _goodses.Add(new ShelfGoods(new Point3D(rowIndex, i + 1, layIndex))); } } public void Sort() { _goodses = _goodses.OrderBy(s => s.GoodsNo).ToList(); } public void Draw() { foreach (ShelfGoods goods in _goodses) { if (ChoosedGoodsEventHanler != null) { goods.ChoosedGoodsEventHanler += ChoosedGoodsEventHanler; } LinePanel.Children.Add(goods); } } public ShelfGoods Find(Point3D point) { return _goodses.FirstOrDefault(s => s.Point3D.IsEquals(point)); } public void Put(List<ShelfGoods> shelfGoodses) { foreach (ShelfGoods goods in shelfGoodses) { ShelfGoods query = Find(goods.Point3D); if (query == null) continue; query.SetGoodsNo(goods.GoodsNo); query.AddProperties(goods.GoodsProperties); query.SetTip(); string color = _warehouseBll.GetColor(goods.GoodsNo); if (!string.IsNullOrEmpty(color)) { query.ChangeBgColor(color); } break; } } }
货架
/// <summary> /// Interaction logic for ShelfLayer.xaml /// </summary> public partial class ShelfLayer : UserControl { private int _colCount; private int _layIndex; private int _rowCount; private List<ShelfLine> _shelfLines ; public ChooseGoodsEventHanler ChoosedGoodsEventHanler = null; public ShelfLayer() { InitializeComponent(); } public int ColCount { get { return _colCount; } } public int LayIndex { get { return _layIndex; } } public int RowCount { get { return _rowCount; } } public void Init(int rowCount, int colCount, int layIndex = 0) { _rowCount = rowCount; _colCount = colCount; _layIndex = layIndex; _shelfLines = new List<ShelfLine>(); for (int i = 0; i < rowCount; i++) { var line = new ShelfLine(i+1, colCount, layIndex); if (ChoosedGoodsEventHanler != null) { line.ChoosedGoodsEventHanler += ChoosedGoodsEventHanler; } _shelfLines.Add(line); } } public void Sort() { _shelfLines = _shelfLines.OrderBy(s => s.RowIndex).ToList(); } public void Draw() { Clear(); foreach (ShelfLine line in _shelfLines) { line.Draw(); LayerPanel.Children.Add(line); } } public void Clear() { LayerPanel.Children.Clear(); } public ShelfGoods Find(Point3D point) { return _shelfLines.Select(line => line.Find(point)).FirstOrDefault(query => query != null); } public void Put(List<ShelfGoods> shelfGoodses) { foreach (ShelfLine line in _shelfLines) { List<ShelfGoods> query = shelfGoodses.Where(s => s.Point3D.PointX == line.RowIndex).ToList(); line.Put(query); } } }
货架列表
this.WareHouseControl.HiddenConfig(true); WareHouseControl.ChoosedGoodsEventHanler += args => { if (args == null) return; if (args.IsOnlySaveWarehouse) { if (MessageBoxEx.Show("您确定选择选择仓库:" + args.WareHouseName + "?", "提示", MessageBoxExButtons.YesNo, MessageBoxExIcon.Question) == true) { _chooseWarehouseId = args.WarehouseId; _chooseWarehouseName = args.WareHouseName; DialogResult = true; } return; } if (!string.IsNullOrEmpty(args.GoodsNo)) { string msg = string.Format("该位置({0},{1},{2})已经存储货物!", args.GoodsPoint3D.PointX, args.GoodsPoint3D.PointY, args.GoodsPoint3D.PointZ); MessageBoxEx.Show(msg, "提示", MessageBoxExButtons.OK, MessageBoxExIcon.Information); return; } _choosedPoint = args.GoodsPoint3D; _shelfNo = WareHouseControl.ShelfNo; _chooseWarehouseId = WareHouseControl.WarehouseId; _chooseWarehouseName = WareHouseControl.WarehouseName; if (_chooseWarehouseId < 0 || string.IsNullOrEmpty(_shelfNo)) return; string message = string.Format("您确定选择选择货架编号:{0},位置:({1},{2},{3})?", _shelfNo, args.GoodsPoint3D.PointX, args.GoodsPoint3D.PointY, args.GoodsPoint3D.PointZ); if (MessageBoxEx.Show(message, "提示", MessageBoxExButtons.YesNo, MessageBoxExIcon.Question) == true) { DialogResult = true; } };
控件调用
作者:Dynamic-xia
博客地址:http://www.cnblogs.com/dynamic-xia
声明:本博客以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。
时间: 2024-11-05 19:35:21