private MDF_Line CreateLine(MDF_ConnectLineModel linemodel) { if (canvasLine == null) { return(null); } if (LineSource == null) { return(null); } if (m_dicitem.Where(p => p.Key == linemodel.From).Count() == 0) { return(null); } if (m_dicitem.Where(p => p.Key == linemodel.To).Count() == 0) { return(null); } ConnectItem from = m_dicitem[linemodel.From]; ConnectItem to = m_dicitem[linemodel.To]; if (from == null || to == null) { return(null); } if (LineCheck != null) { if (!LineCheck.AddLineCheck(from.DataContext as MDF_ConnectItemModel, to.DataContext as MDF_ConnectItemModel)) { return(null); } } //判断是否已存在,如果存在则执行创建线 var v = canvasLine.Children.FirstOrDefault(p => ((p as MDF_Line).DataContext as MDF_ConnectLineModel).From == linemodel.From && ((p as MDF_Line).DataContext as MDF_ConnectLineModel).To == linemodel.To); if (v != null) { return(null); } MDF_Line line = new MDF_Line(); line.SetBinding(MDF_Line.StyleProperty, new Binding() { Source = this, Path = new PropertyPath("LineStyle") }); line.DataContext = linemodel; canvasLine.Children.Add(line); ComputeLineInfo(line); line.LineSelected += line_LineSelected; return(line); }
private void AddItem(MDF_ConnectItemModel item) { if (item == null) { return; } if (m_dicitem.ContainsKey(item.Key)) { return; } Grid grd = CreateGrid(item); grd.RowDefinitions.Add(new RowDefinition()); ConnectItem citem = new ConnectItem(); citem.SetBinding(ConnectItem.StyleProperty, new Binding() { Source = this, Path = new PropertyPath("ConnectItemStyle") }); citem.SetBinding(Grid.RowProperty, new Binding() { Source = item, Path = new PropertyPath("RealRowIndex") }); citem.DataContext = item; if (citem.Height < 20 || double.IsNaN(citem.Height)) { citem.Height = 20; } if (citem.Width < 20 || double.IsNaN(citem.Width)) { citem.Width = 20; } m_dicitem.Add(item.Key, citem); grd.Children.Add(citem); citem.ItemChecked += citem_ItemChecked; }
//计算线位置 private void ComputeLineInfo(MDF_Line line) { if (line == null) { return; } MDF_ConnectLineModel model = line.DataContext as MDF_ConnectLineModel; if (model == null) { return; } if (gridMain == null || canvasLine == null) { return; } if (m_dicitem.Count == 0) { return; } if (m_dicitem.Keys.Contains(model.From) && m_dicitem.Keys.Contains(model.To)) { ConnectItem from = m_dicitem[model.From]; ConnectItem to = m_dicitem[model.To]; if (from == null || to == null) { return; } //计算控件坐标点 if (from.ActualHeight == 0) { return; } if (canvasLine.ActualHeight == 0) { return; } List <Point> pfromlist = SLControlUnity.GetControlPointLeftRight(canvasLine, from); List <Point> ptolist = SLControlUnity.GetControlPointLeftRight(canvasLine, to); Tuple <Point, Point> nearpoint = SLControlUnity.ComplateNearPoint(pfromlist, ptolist); //得到最近两个点 double len = SLControlUnity.Distance(nearpoint.Item1, nearpoint.Item2); //得到两点最近距离 double angle = SLControlUnity.SlopeAngle(nearpoint.Item1, nearpoint.Item2); //计算两点倾斜角度 Canvas.SetLeft(line, nearpoint.Item1.X); Canvas.SetTop(line, nearpoint.Item1.Y - line.ActualHeight / 2); line.Width = len; CompositeTransform transform = line.RenderTransform as CompositeTransform; if (transform == null) { transform = new CompositeTransform(); line.RenderTransform = transform; } line.RenderTransformOrigin = new Point(0, 0.5); //transform.CenterX = 0; //transform.CenterY = 0.5; transform.Rotation = angle; } }