public BoxGroupViewModel(LEDBoxGroup boxGroup)
        {
            _boxGroup = boxGroup;
            _boxCount = _boxGroup.Boxes.Count;
            _COMIndex = _boxGroup.COMIndex;
            _senderIndex = (byte)(_boxGroup.SenderIndex + 1);
            _portIndex = (byte)(_boxGroup.PortIndex + 1);
            _indexLocation = GetIndexLocation(_boxGroup);

            LEDBoxes = new ObservableCollection<BoxViewModel>();

            var firstBox = _boxGroup.Boxes.FirstOrDefault(b => b.ConnectIndex == 0);
            if (firstBox == null)
            {
                return;
            }
            if (firstBox.X == 0)
            {
                _boxGroup.Boxes = _boxGroup.Boxes.OrderBy(b => b.ConnectIndex).ToList();
            }
            else
            {
                _boxGroup.Boxes = _boxGroup.Boxes.OrderByDescending(b => b.ConnectIndex).ToList();
            }

            foreach (var item in _boxGroup.Boxes)
            {
                LEDBoxes.Add(new BoxViewModel(item));
            }
        }
 private string GetIndexLocation(LEDBoxGroup group)
 {
     return string.Format("{0}-S{1}-P{2}", group.COMIndex, group.SenderIndex+1, group.PortIndex+1);
 }
 public BoxGroupToolItemViewModel(LEDBoxGroup boxGroup)
 {
     _boxGroup = boxGroup;
     _indexLocation = GetIndexLocation(_boxGroup);
     _boxCount = _boxGroup.Boxes.Count;
 }
        private static ObservableCollection<BoxGroupViewModel> LoadComponentXml(string filePath)
        {
            var boxGroupViewModelList = new ObservableCollection<BoxGroupViewModel>();
            XDocument doc = XDocument.Load(filePath);
            if (doc != null)
            {
                IEnumerable<XElement> elementlist = doc.Root.Elements("BoxGroup");

                foreach (var item in elementlist)
                {

                    ObservableCollection<BoxViewModel> boxViewModelList = new ObservableCollection<BoxViewModel>();

                    List<XElement> infoElementList = item.Elements("BoxGroupInfo").ToList();
                    List<XElement> pointElementList = item.Elements("Boxes").ToList();

                    for (int count = 0; count < infoElementList.Count; count++)
                    {

                        LEDBoxGroup boxGroup = new LEDBoxGroup();
                        List<LEDBox> boxList = new List<LEDBox>();
                        foreach (var point in pointElementList[count].Elements("Box"))
                        {
                            XAttribute COMIndexAttr = point.Attribute("COMIndex");
                            XAttribute SenderIndexAttr = point.Attribute("SenderIndex");
                            XAttribute PortIndexAttr = point.Attribute("PortIndex");
                            XAttribute ConnectIndexAttr = point.Attribute("ConnectIndex");
                            XAttribute WidthAttr = point.Attribute("Width");
                            XAttribute HeightAttr = point.Attribute("Height");
                            XAttribute XInPortAttr = point.Attribute("XInPort");
                            XAttribute YInPortAttr = point.Attribute("YInPort");
                            XAttribute XAttr = point.Attribute("X");
                            XAttribute YAttr = point.Attribute("Y");

                            LEDBox box = new LEDBox();
                            box.COMIndex = COMIndexAttr.Value;
                            box.SenderIndex = Convert.ToByte(SenderIndexAttr.Value);
                            box.PortIndex = Convert.ToByte(PortIndexAttr.Value);
                            box.ConnectIndex = Convert.ToByte(ConnectIndexAttr.Value);
                            box.Width = Convert.ToDouble(WidthAttr.Value);
                            box.Height = Convert.ToDouble(HeightAttr.Value);
                            box.XInPort = Convert.ToUInt16(XInPortAttr.Value);
                            box.YInPort = Convert.ToUInt16(YInPortAttr.Value);
                            box.X = Convert.ToUInt16(XAttr.Value);
                            box.Y = Convert.ToUInt16(YAttr.Value);

                            boxList.Add(box);

                            //BoxViewModel boxViewModel = new BoxViewModel(box);
                            //boxViewModelList.Add(boxViewModel);
                        }
                        boxGroup.COMIndex = boxList[0].COMIndex;
                        boxGroup.SenderIndex = boxList[0].SenderIndex;
                        boxGroup.PortIndex = boxList[0].PortIndex;
                        boxGroup.Boxes = boxList;

                        BoxGroupViewModel elementViewModel = new BoxGroupViewModel(boxGroup);

                        XAttribute sysIndexLocationAttr = infoElementList[count].Attribute("IndexLocation");
                        if (sysIndexLocationAttr != null)
                            elementViewModel.IndexLocation = sysIndexLocationAttr.Value;

                        XAttribute sysElementPxPointXAttr = infoElementList[count].Attribute("ElementPxPointX");
                        if (sysElementPxPointXAttr != null)
                            elementViewModel.ElementPxPointX = double.Parse(sysElementPxPointXAttr.Value);

                        XAttribute elementPxPointYAttr = infoElementList[count].Attribute("ElementPxPointY");
                        if (elementPxPointYAttr != null)
                            elementViewModel.ElementPxPointY = double.Parse(elementPxPointYAttr.Value);

                        boxGroupViewModelList.Add(elementViewModel);
                    }


                    #region load
                    //foreach (var infoElement in infoElementList)
                    //{

                    //    LEDBoxGroup groupitem = new LEDBoxGroup();


                    //    XAttribute sysIndexLocationAttr = infoElement.Attribute("IndexLocation");
                    //    if (sysIndexLocationAttr != null)
                    //        elementViewModel.IndexLocation = sysIndexLocationAttr.Value;

                    //    XAttribute sysElementPxPointXAttr = infoElement.Attribute("ElementPxPointX");
                    //    if (sysElementPxPointXAttr != null)
                    //        elementViewModel.ElementPxPointX = double.Parse(sysElementPxPointXAttr.Value);

                    //    XAttribute elementPxPointYAttr = infoElement.Attribute("ElementPxPointY");
                    //    if (elementPxPointYAttr != null)
                    //        elementViewModel.ElementPxPointY = double.Parse(elementPxPointYAttr.Value);

                    //}

                    //IEnumerable<XElement> pointElementList = item.Elements("Boxes");
                    //foreach (var pointElement in pointElementList)
                    //{

                    //    foreach (var point in pointElement.Elements("Box"))
                    //    {
                    //        XAttribute COMIndexAttr = point.Attribute("COMIndex");
                    //        XAttribute SenderIndexAttr = point.Attribute("SenderIndex");
                    //        XAttribute PortIndexAttr = point.Attribute("PortIndex");
                    //        XAttribute ConnectIndexAttr = point.Attribute("ConnectIndex");
                    //        XAttribute WidthAttr = point.Attribute("Width");
                    //        XAttribute HeightAttr = point.Attribute("Height");
                    //        XAttribute XInPortAttr = point.Attribute("XInPort");
                    //        XAttribute YInPortAttr = point.Attribute("YInPort");
                    //        XAttribute XAttr = point.Attribute("X");
                    //        XAttribute YAttr = point.Attribute("Y");

                    //        LEDBox box = new LEDBox();
                    //        box.COMIndex = COMIndexAttr.Value;
                    //        box.SenderIndex = Convert.ToByte(SenderIndexAttr.Value);
                    //        box.PortIndex = Convert.ToByte(PortIndexAttr.Value);
                    //        box.ConnectIndex = Convert.ToByte(ConnectIndexAttr.Value);
                    //        box.Width = Convert.ToDouble(WidthAttr.Value);
                    //        box.Height = Convert.ToDouble(HeightAttr.Value);
                    //        box.XInPort = Convert.ToUInt16(XInPortAttr.Value);
                    //        box.YInPort = Convert.ToUInt16(YInPortAttr.Value);
                    //        box.X = Convert.ToUInt16(XAttr.Value);
                    //        box.Y = Convert.ToUInt16(YAttr.Value);

                    //        BoxViewModel boxViewModel = new BoxViewModel(box);
                    //        boxViewModelList.Add(boxViewModel);
                    //    }
                    //}

                    #endregion

                    //elementViewModel.LEDBoxes = boxViewModelList;
                    //boxGroupViewModelList.Add(elementViewModel);
                }
            }
            return boxGroupViewModelList;
        }