private void SetTextBlockProperty(ScaleLabelDirection pDirection, TextBlock tb, int i, double unit) { double left = 0, top = 0; switch (pDirection) { case ScaleLabelDirection.Left: tb.RenderTransform = new TranslateTransform { Y = -(tb.ActualHeight / 2) }; left = _scaleCanvas.ActualWidth - tb.ActualWidth - GetSpan(); top = i * unit; Canvas.SetLeft(tb, left); Canvas.SetTop(tb, top); break; case ScaleLabelDirection.Top: tb.RenderTransform = new TranslateTransform { X = -(tb.ActualWidth / 2) }; left = i * unit; top = 0 - GetSpan(); Canvas.SetTop(tb, top); Canvas.SetLeft(tb, left); break; case ScaleLabelDirection.Right: tb.RenderTransform = new TranslateTransform { Y = -(tb.ActualHeight / 2) }; left = 0 + GetSpan(); top = i * unit; Canvas.SetLeft(tb, left); Canvas.SetTop(tb, top); break; case ScaleLabelDirection.Bottom: tb.RenderTransform = new TranslateTransform { X = -(tb.ActualWidth / 2) }; left = i * unit; top = 0 + GetSpan(); Canvas.SetTop(tb, top); Canvas.SetLeft(tb, left); break; } if (LabelStyle != null) { tb.Style = LabelStyle; } else { var bind = new Binding("Foreground") { Source = this }; tb.SetBinding(TextBlock.ForegroundProperty, bind); } tb.Tag = new[] { left, top }; }
/// <summary> /// /// </summary> /// <param name="pDirection"></param> /// <param name="vunit"></param> /// <param name="unit"></param> private void GetUnit(ScaleLabelDirection pDirection, out double vunit, out double unit) { vunit = (EndValue - BeginValue) / (ScaleLabelCount - 1); if (pDirection == ScaleLabelDirection.Top || pDirection == ScaleLabelDirection.Bottom) { unit = _rangePanel.ActualWidth / (ScaleLabelCount - 1); } else { unit = _rangePanel.ActualHeight / (ScaleLabelCount - 1); } }
/// <summary> /// 添加刻度的线 /// </summary> /// <param name="pDirection"></param> /// <param name="i"></param> /// <param name="unit"></param> private void SetLineProperty(ScaleLabelDirection pDirection, int i, double unit) { switch (pDirection) { case ScaleLabelDirection.Left: SetLeftLineProperty(i, unit); break; case ScaleLabelDirection.Top: SetTopLineProperty(i, unit); break; case ScaleLabelDirection.Right: SetRightLineProperty(i, unit); break; case ScaleLabelDirection.Bottom: SetBottomLineProperty(i, unit); break; } }
private void UpdateScales2(ScaleLabelDirection pDirection, IList <string> valStrs, double unit) { if (_scaleCanvas.Children.Count == 0) { for (int i = 0; i < ScaleLabelCount; i++) { var tb = new TextBlock { Text = valStrs[i], FontSize = 11 }; _scaleCanvas.Children.Add(tb); SetLineProperty(pDirection, i, unit); } } else //因为wpf中控件没有render获取不到它的宽和高,所以每次刻度长度变化时有个小bug @? { var tl = new List <TextBlock>(); for (int i = 0; i < _scaleCanvas.Children.Count; i++) { TextBlock tb = _scaleCanvas.Children[i] as TextBlock; if (tb != null) { tl.Add(tb); } } _scaleCanvas.Children.Clear(); for (int i = 0; i < ScaleLabelCount; i++) { tl[i].Text = valStrs[i]; _scaleCanvas.Children.Add(tl[i]); SetLineProperty(pDirection, i, unit); SetTextBlockProperty(Direction, tl[i], i, GetUnitSize()); } } }