private async void cbDiagramSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            System.Collections.Generic.List<UIElement> Removelist = new System.Collections.Generic.List<UIElement>();

            int planeid= (cbDiagramSelect.SelectedItem as tblERPlane).PlaneID;
             this.imgPic.Source = new BitmapImage(new Uri("/Diagrams/" + (cbDiagramSelect.SelectedItem as tblERPlane).PlaneID+".png", UriKind.Relative));
             foreach (UIElement c in grdDeviceLayer.Children)
             {
                 if (c is Image) continue;
                 Removelist.Add(c);
                 //grdDeviceLayer.Children.Remove(c);
             }

            foreach(UIElement ui in Removelist)
                grdDeviceLayer.Children.Remove(ui);
            Removelist.Clear();
             #region  Door
             var q = from n in db.GetTblControllerConfigQuery() where (n.ControlType == 1 || n.ControlType == 2  ) && n.PlaneID== (cbDiagramSelect.SelectedValue as tblERPlane).PlaneID   select n;
             var res= await   DB.LoadAsync<tblControllerConfig>(db, q);

             foreach (tblControllerConfig tbl in res)
             {
                DOOR item = new DOOR();
                item.Name = "Door"+tbl.ControlID;
             //  tblItem info = CreateInputItemInfo("DOOR", "DOOR", "", 1, 0);
                
                item.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                item.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                item.SetValue(Grid.MarginProperty, new Thickness(tbl.X??0, tbl.Y??0, 0, 0));
              
              //  item.Margin = new Thickness(0);

              // grdSetting.DataContext = 
                item.DataContext = tbl;
                 
                item.SetDefaultColor();
                

                this.grdDeviceLayer.Children.Add(item);

                item.MouseLeftButtonDown += selectedDevice_MouseLeftButtonDown;
                item.MouseLeftButtonUp += selectedDevice_MouseLeftButtonUp;
                item.MouseMove += selectedDevice_MouseMove;
               // ctl = item;

             }
             #endregion
            #region          CCTV
             var q2 = from n in db.GetTblCCTVConfigQuery() where n.PlaneID == planeid select n;
             var res2 = await DB.LoadAsync<tblCCTVConfig>(db, q2);
             foreach (tblCCTVConfig tbl in res2)
             {
                 CCTV item = new CCTV();
                 item.Name = "CCTV" + tbl.CCTVID;
                 item.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                 item.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                 item.SetValue(Grid.MarginProperty, new Thickness(tbl.X, tbl.Y, 0, 0));
                 //  item.Margin = new Thickness(0);

                 //  grdSetting.DataContext = 
                 item.DataContext = tbl;



                 this.grdDeviceLayer.Children.Add(item);

                 item.MouseLeftButtonDown += selectedDevice_MouseLeftButtonDown;
                 item.MouseLeftButtonUp += selectedDevice_MouseLeftButtonUp;
                 item.MouseMove += selectedDevice_MouseMove;
             }

            #endregion
#region AI DI DO  
             var qai = from n in db.GetTblItemConfigQuery() where n.IsShow && n.tblItemGroup.PlaneID==planeid   select n;
             var resqai = await DB.LoadAsync<tblItemConfig>(db, qai);

             foreach (tblItemConfig tbl in resqai)
             {
                 if (tbl.Type == "AI")
                 {
                     AI ai = new AI();
                     ai.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                     ai.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                     ai.SetValue(Grid.MarginProperty, new Thickness(tbl.X ?? 0, tbl.Y ?? 0, 0, 0));
                     ai.Content ="AI:"+ tbl.ItemName;
                     //CompositeTransform transform = new CompositeTransform() { Rotation = tbl.Rotation ?? 0, ScaleX = tbl.ScaleX ?? 1, ScaleY = tbl.ScaleY ?? 1 };
                     //ai.RenderTransform = transform;
                     ai.DataContext = tbl;    //new SecureServer.BindingData.ItemBindingData { ColorString = "Green", Content = "100V", ItemID =1, Type=  "AI" };
                     this.grdDeviceLayer.Children.Add(ai);
                     ai.MouseLeftButtonDown += selectedDevice_MouseLeftButtonDown;
                     ai.MouseLeftButtonUp += selectedDevice_MouseLeftButtonUp;
                     ai.MouseMove += selectedDevice_MouseMove;
                 }
                 else if (tbl.Type == "DI")
                 {
                     DI di = new DI();
                     di.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                     di.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                     di.SetValue(Grid.MarginProperty, new Thickness(tbl.X ?? 0, tbl.Y ?? 0, 0, 0));
                     di.Content ="DI:"+ tbl.ItemName;
                     
                     //CompositeTransform transform = new CompositeTransform() { Rotation = tbl.Rotation ?? 0, ScaleX = tbl.ScaleX ?? 1, ScaleY = tbl.ScaleY ?? 1 };
                     //ai.RenderTransform = transform;
                     di.DataContext = tbl;    //new SecureServer.BindingData.ItemBindingData { ColorString = "Green", Content = "100V", ItemID =1, Type=  "AI" };
                     this.grdDeviceLayer.Children.Add(di);
                     di.MouseLeftButtonDown += selectedDevice_MouseLeftButtonDown;
                     di.MouseLeftButtonUp += selectedDevice_MouseLeftButtonUp;
                     di.MouseMove += selectedDevice_MouseMove;

                 }
                 else if (tbl.Type == "DO")
                 {

                     DO di = new DO();
                     di.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                     di.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                     di.SetValue(Grid.MarginProperty, new Thickness(tbl.X ?? 0, tbl.Y ?? 0, 0, 0));
                     di.Content ="DO:"+ tbl.ItemName;
                     //CompositeTransform transform = new CompositeTransform() { Rotation = tbl.Rotation ?? 0, ScaleX = tbl.ScaleX ?? 1, ScaleY = tbl.ScaleY ?? 1 };
                     //ai.RenderTransform = transform;
                     di.DataContext = tbl;    //new SecureServer.BindingData.ItemBindingData { ColorString = "Green", Content = "100V", ItemID =1, Type=  "AI" };
                     this.grdDeviceLayer.Children.Add(di);
                     di.Width = 52;
                     di.Height = 40;
                     di.MouseLeftButtonDown += selectedDevice_MouseLeftButtonDown;
                     di.MouseLeftButtonUp += selectedDevice_MouseLeftButtonUp;
                     di.MouseMove += selectedDevice_MouseMove;

                 }
                 
             }

#endregion

#region ItemGroup
             var qgrouo = from n in db.GetTblItemGroupQuery() where n.IsShow && n.PlaneID == planeid select n;
             var resgroup = await DB.LoadAsync<tblItemGroup>(db, qgrouo);
             foreach (tblItemGroup tbl in resgroup)
             {
                 ItemGroup grp = new ItemGroup();
                 grp.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                 grp.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                 grp.SetValue(Grid.MarginProperty, new Thickness(tbl.X  , tbl.Y , 0, 0));
                 grp.Content ="Group:"+ tbl.GroupName;
                
                 grp.DataContext = tbl;
                 this.grdDeviceLayer.Children.Add(grp);
                 grp.MouseLeftButtonDown += selectedDevice_MouseLeftButtonDown;
                 grp.MouseLeftButtonUp += selectedDevice_MouseLeftButtonUp;
                 grp.MouseMove += selectedDevice_MouseMove;
             }


#endregion

        }
        async void PlaceItemGroup()
        {
            var q = from n in db.GetTblItemGroupQuery() where n.PlaneID == this.PlaneID && n.IsShow select n ;
            var res = await db.LoadAsync<tblItemGroup>(q);
            foreach (tblItemGroup tbl in res)
            {
                ItemGroup item = new ItemGroup() { Name = "Grp" + tbl.GroupID };

                (item as Control).HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                (item as Control).VerticalAlignment = System.Windows.VerticalAlignment.Top;
                (item as Control).SetValue(Grid.MarginProperty, new Thickness(tbl.X , tbl.Y , 0, 0));
                CompositeTransform transform = new CompositeTransform() { Rotation = tbl.Rotation , ScaleX = tbl.ScaleX  , ScaleY = tbl.ScaleY   };
                (item as Control).RenderTransform = transform;
                if (ItemGroupBindingDatas != null)
                {
                    ItemGroupBindingData data =ItemGroupBindingDatas.FirstOrDefault(n => n.PlaneID == this.PlaneID && n.GroupID == tbl.GroupID);
                  //  data.Content = "hello";
                    (item as Control).DataContext = data;// ItemGroupBindingDatas[0];
                 
                }
                item.MouseLeftButtonDown += itemGroup_MouseLeftButtonDown;
                this.Canvas.Children.Add(item);
            }
        }