//private int CellsColumnIndex = 0;
        private void CGridItemsSource_ExtendedItemsChanged(object item, bool AddOrRemove)
        {
            if (!(item is DeviceModel))
            {
                return;
            }
            DeviceModel device = item as DeviceModel;

            if (AddOrRemove)
            {
                InnerDeviceList.Add(device);
                ConstructionHelper.Add(device);
                foreach (DeviceInterface element in device.InterfaceList)
                {
                    ConstructionHelper.Add(element);
                }
                foreach (List <CellState> RowList in ViewModel.cellsState)
                {
                    for (int i = 0; i < device.InterfaceCount + 1; i++)
                    {
                        RowList.Add(new Model.CellState()
                        {
                            RowState = false, ColumnState = false
                        });
                    }
                }
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    RowCreateHelper.Add(new Model.CellState()
                    {
                        RowState = false, ColumnState = false
                    });
                    PositionHelper.Add(PositionHelperIndex++);
                }
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    ViewModel.cellsState.Add(new List <Model.CellState>(RowCreateHelper));
                }
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    this.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = new GridLength(20, GridUnitType.Auto)
                    });
                    this.RowDefinitions.Add(new RowDefinition()
                    {
                        Height = new GridLength(20, GridUnitType.Auto)
                    });
                }
                foreach (List <Control> RowList in CellsControl)
                {
                    int RowIndex = CellsControl.IndexOf(RowList);
                    for (int i = 0; i < device.InterfaceCount + 1; i++)
                    {
                        object TempDev = ConstructionHelper[RowIndex] as object;
                        if (i == 0)
                        {
                            int     ColumnIndex = ConstructionHelper.IndexOf(device);
                            Control CellControl = CellFactory.CreateCell(TempDev, device, RowIndex, ColumnIndex);
                            this.Children.Add(CellControl);
                            RowList.Add(CellControl);
                            Grid.SetColumn(CellControl, this.ColumnDefinitions.Count + i - device.InterfaceCount - 1);
                            Grid.SetRow(CellControl, PositionHelper[RowIndex]);
                        }
                        else
                        {
                            int     ColumnIndex = ConstructionHelper.IndexOf(device.InterfaceList[i - 1]);
                            Control CellControl = CellFactory.CreateCell(TempDev, device.InterfaceList[i - 1], RowIndex, ColumnIndex);
                            this.Children.Add(CellControl);
                            RowList.Add(CellControl);
                            Grid.SetColumn(CellControl, this.ColumnDefinitions.Count + i - device.InterfaceCount - 1);
                            Grid.SetRow(CellControl, PositionHelper[RowIndex]);
                        }
                    }
                }

                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    List <Control> tempRow = new List <Control>();
                    if (i == 0)
                    {
                        for (int ColumnIndex = 0; ColumnIndex < ConstructionHelper.Count; ColumnIndex++)
                        {
                            Control CellControl = CellFactory.CreateCell(device, ConstructionHelper[ColumnIndex], CellsControl.Count, ColumnIndex);
                            tempRow.Add(CellControl);
                            this.Children.Add(CellControl);
                            Grid.SetColumn(CellControl, PositionHelper[ColumnIndex]);
                            Grid.SetRow(CellControl, RowDefinitions.Count - device.InterfaceCount - 1 + i);
                        }
                    }
                    else
                    {
                        for (int ColumnIndex = 0; ColumnIndex < ConstructionHelper.Count; ColumnIndex++)
                        {
                            Control CellControl = CellFactory.CreateCell(device.InterfaceList[i - 1], ConstructionHelper[ColumnIndex], CellsControl.Count, ColumnIndex);
                            tempRow.Add(CellControl);
                            this.Children.Add(CellControl);
                            Grid.SetColumn(CellControl, PositionHelper[ColumnIndex]);
                            Grid.SetRow(CellControl, RowDefinitions.Count - device.InterfaceCount - 1 + i);
                        }
                    }
                    CellsControl.Add(tempRow);
                }
                Debug.WriteLine("CellsState measure " + ViewModel.cellsState.Count + " " + ViewModel.cellsState.First().Count);
                Debug.WriteLine("CellsControl measure " + CellsControl.Count + " " + CellsControl.First().Count);
                Debug.WriteLine("grid children count " + this.Children.Count);
                int r = 0;
                foreach (Control cell in this.Children)
                {
                    cell.Visibility = Visibility.Visible;
                    if (cell is CToggleBtn)
                    {
                        Debug.Write("one ctoggleBtn" + (++r));
                    }
                }
            }
            else
            {
                int Index       = InnerDeviceList.IndexOf(device);
                int RemoveIndex = ConstructionHelper.IndexOf(device);
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    ViewModel.cellsState.RemoveAt(RemoveIndex);
                }
                foreach (List <CellState> RowList in ViewModel.cellsState)
                {
                    for (int i = 0; i < device.InterfaceCount + 1; i++)
                    {
                        RowList.RemoveAt(RemoveIndex);
                    }
                }
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    ConstructionHelper.RemoveAt(RemoveIndex);
                    RowCreateHelper.RemoveAt(RemoveIndex);
                    PositionHelper.RemoveAt(RemoveIndex);
                }
                InnerDeviceList.Remove((DeviceModel)item);
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    foreach (Control Cell in CellsControl[RemoveIndex])
                    {
                        this.Children.Remove(Cell);
                    }
                    CellsControl.RemoveAt(RemoveIndex);
                }
                foreach (List <Control> rowlist in CellsControl)
                {
                    for (int i = 0; i < device.InterfaceCount + 1; i++)
                    {
                        this.Children.Remove(rowlist[RemoveIndex]);
                        rowlist.RemoveAt(RemoveIndex);
                    }
                }
                if (ViewModel.cellsState.Count == 0)
                {
                    return;
                }
                Debug.WriteLine("CellsState measure " + ViewModel.cellsState.Count + " " + ViewModel.cellsState.First().Count);
                Debug.WriteLine("CellsControl measure " + CellsControl.Count + " " + CellsControl.First().Count);
                Debug.WriteLine("grid children count " + this.Children.Count);
                int r = 0;
                foreach (Control cell in this.Children)
                {
                    cell.Visibility = Visibility.Visible;
                    if (cell is CToggleBtn)
                    {
                        Debug.Write("one ctoggleBtn" + (++r));
                    }
                }
            }
        }
        private void CGridItemsSource_ExtendedItemsChanged(object item, bool AddOrRemove)
        {
            if (!(item is DeviceModel))
            {
                return;
            }
            if (AddOrRemove)
            {
                DeviceModel device = item as DeviceModel;
                InnerDeviceList.Add(device);
                ConstructionHelper.Add(device);
                foreach (DeviceInterface element in device.InterfaceList)
                {
                    ConstructionHelper.Add(element);
                }
                foreach (List <CellState> RowList in ViewModel.cellsState)
                {
                    for (int i = 0; i < device.InterfaceCount + 1; i++)
                    {
                        RowList.Add(new Model.CellState()
                        {
                            RowState = false, ColumnState = false
                        });
                    }
                }
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    RowCreateHelper.Add(new Model.CellState()
                    {
                        RowState = false, ColumnState = false
                    });
                }
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    ViewModel.cellsState.Add(new List <Model.CellState>(RowCreateHelper));
                }
                foreach (List <CellState> RowList in ViewModel.cellsState)
                {
                    foreach (var e in RowList)
                    {
                        Debug.Write("1--");
                    }
                    Debug.Write("\n");
                }

                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    this.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = new GridLength(0, GridUnitType.Auto)
                    });
                    this.RowDefinitions.Add(new RowDefinition()
                    {
                        Height = new GridLength(0, GridUnitType.Auto)
                    });
                }

                foreach (List <Control> RowList in CellsControl)
                {
                    int RowIndex = CellsControl.IndexOf(RowList);
                    for (int i = 0; i < device.InterfaceCount + 1; i++)
                    {
                        if (ConstructionHelper[RowIndex] is DeviceModel)
                        {
                            DeviceModel TempDev = ConstructionHelper[RowIndex] as DeviceModel;
                            if (i == 0)
                            {
                                CToggleBtn TempBtn = new CToggleBtn()
                                {
                                };
                                TempBtn.ToolTip = string.Format(TempDev.DeviceName + "&&" + device.DeviceName);

                                int ColumnIndex = ConstructionHelper.IndexOf(device);
                                Grid.SetColumn(TempBtn, ColumnIndex);
                                Grid.SetRow(TempBtn, RowIndex);
                                Binding B1 = new Binding("RowState")
                                {
                                    Source = cellsState[RowIndex][ColumnIndex]
                                };
                                Binding B2 = new Binding("Column")
                                {
                                    Source = cellsState[RowIndex][ColumnIndex]
                                };

                                MultiBinding MBinding = new MultiBinding()
                                {
                                    Mode = BindingMode.OneWay
                                };
                                MBinding.Bindings.Add(B1);
                                MBinding.Bindings.Add(B2);
                                MBinding.Converter = Converter.CellStateConverter;

                                TempBtn.SetBinding(CToggleBtn.ChangedIconProperty, MBinding);
                            }
                            else
                            {
                                CLabel TempCLabel = new CLabel()
                                {
                                    IsCommon = false
                                };
                                TempCLabel.ToolTip = string.Format(TempDev.DeviceName);
                                int ColumnIndex = ConstructionHelper.IndexOf(device.InterfaceList[i - 1]);
                                Grid.SetColumn(TempCLabel, ColumnIndex);
                                Grid.SetRow(TempCLabel, RowIndex);

                                Binding B1 = new Binding("RowState")
                                {
                                    Source = cellsState[RowIndex][ColumnIndex]
                                };
                                Binding B2 = new Binding("Column")
                                {
                                    Source = cellsState[RowIndex][ColumnIndex]
                                };

                                MultiBinding MBinding = new MultiBinding()
                                {
                                    Mode = BindingMode.OneWay
                                };
                                MBinding.Bindings.Add(B1);
                                MBinding.Bindings.Add(B2);
                                MBinding.Converter = Converter.CellStateConverter;
                                TempCLabel.SetBinding(CLabel.IsSelectedProperty, MBinding);
                            }
                        }
                        else
                        {
                            DeviceInterface TempInterface = ConstructionHelper[RowIndex] as DeviceInterface;
                            if (i == 0)
                            {
                                CLabel TempCLabel = new CLabel()
                                {
                                    IsCommon = false
                                };
                                TempCLabel.ToolTip = string.Format(device.DeviceName);
                                int ColumnIndex = ConstructionHelper.IndexOf(device);

                                Grid.SetColumn(TempCLabel, ColumnIndex);
                                Grid.SetRow(TempCLabel, RowIndex);

                                Binding B1 = new Binding("RowState")
                                {
                                    Source = cellsState[RowIndex][ColumnIndex]
                                };
                                Binding B2 = new Binding("Column")
                                {
                                    Source = cellsState[RowIndex][ColumnIndex]
                                };

                                MultiBinding MBinding = new MultiBinding()
                                {
                                    Mode = BindingMode.OneWay
                                };
                                MBinding.Bindings.Add(B1);
                                MBinding.Bindings.Add(B2);
                                MBinding.Converter = Converter.CellStateConverter;

                                TempCLabel.SetBinding(CToggleBtn.ChangedIconProperty, MBinding);
                            }
                            else
                            {
                                CLabel TempCLabel = new CLabel()
                                {
                                    IsCommon = true
                                };
                                TempCLabel.ToolTip = string.Format(TempInterface.InterfaceName + "=>" + device.InterfaceList[i - 1].InterfaceName);
                                int ColumnIndex = ConstructionHelper.IndexOf(device.InterfaceList[i - 1]);
                                Grid.SetColumn(TempCLabel, ColumnIndex);
                                Grid.SetRow(TempCLabel, RowIndex);
                                Binding B1 = new Binding("RowState")
                                {
                                    Source = cellsState[RowIndex][ColumnIndex]
                                };
                                Binding B2 = new Binding("Column")
                                {
                                    Source = cellsState[RowIndex][ColumnIndex]
                                };

                                MultiBinding MBinding = new MultiBinding()
                                {
                                    Mode = BindingMode.OneWay
                                };
                                MBinding.Bindings.Add(B1);
                                MBinding.Bindings.Add(B2);
                                MBinding.Converter = Converter.CellStateConverter;
                                TempCLabel.SetBinding(CLabel.IsSelectedProperty, MBinding);
                            }
                        }
                    }
                }

                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    if (i == 0)
                    {
                        foreach (var e in ConstructionHelper)
                        {
                            CToggleBtn TempBtn = new CToggleBtn()
                            {
                            };
                        }
                    }
                    else
                    {
                    }
                }
            }
            else
            {
                DeviceModel device      = item as DeviceModel;
                int         Index       = InnerDeviceList.IndexOf(device);
                int         RemoveIndex = ConstructionHelper.IndexOf(device);
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    ViewModel.cellsState.RemoveAt(RemoveIndex);
                }
                foreach (List <CellState> RowList in ViewModel.cellsState)
                {
                    for (int i = 0; i < device.InterfaceCount + 1; i++)
                    {
                        RowList.RemoveAt(RemoveIndex);
                    }
                }
                for (int i = 0; i < device.InterfaceCount + 1; i++)
                {
                    ConstructionHelper.RemoveAt(RemoveIndex);
                    RowCreateHelper.RemoveAt(RemoveIndex);
                }
                InnerDeviceList.Remove((DeviceModel)item);
                foreach (List <CellState> RowList in ViewModel.cellsState)
                {
                    foreach (var e in RowList)
                    {
                        Debug.Write("1--");
                    }
                    Debug.Write("\n");
                }
            }
        }