internal void Draw() { if (!IsControlLaoded) return; //No cache for you gauge :( kill and redraw please foreach (var child in Canvas.Children.Cast<UIElement>() .Where(x => !Equals(x, Stick) && !(x is AngularSection) && !(x is PieSlice)).ToArray()) Canvas.Children.Remove(child); Wedge = Wedge > 360 ? 360 : (Wedge < 0 ? 0 : Wedge); var fromAlpha = (360-Wedge)*.5; var toAlpha = 360 - fromAlpha; var d = ActualWidth < ActualHeight ? ActualWidth : ActualHeight; Stick.Height = d*.5*.8; Stick.Width = Stick.Height*.2; Canvas.SetLeft(Stick, ActualWidth*.5 - Stick.Width*.5); Canvas.SetTop(Stick, ActualHeight*.5 - Stick.Height*.9); var ticksHi = d*.5; var ticksHj = d*.47; var labelsHj = d*.44; foreach (var section in Sections) { PieSlice slice; section.Owner = this; if (!Slices.TryGetValue(section, out slice)) { slice = new PieSlice(); Slices[section] = slice; } var p = (Canvas)section.Parent; if (p != null) p.Children.Remove(section); Canvas.Children.Add(section); var ps = (Canvas)slice.Parent; if (ps != null) ps.Children.Remove(slice); Canvas.Children.Add(slice); } UpdateSections(); for (var i = FromValue; i <= ToValue; i += TicksStep) { var alpha = LinearInterpolation(fromAlpha, toAlpha, FromValue, ToValue, i) + 90; var tick = new Line { X1 = ActualWidth*.5 + ticksHi*Math.Cos(alpha*Math.PI/180), X2 = ActualWidth*.5 + ticksHj*Math.Cos(alpha*Math.PI/180), Y1 = ActualHeight*.5 + ticksHi*Math.Sin(alpha*Math.PI/180), Y2 = ActualHeight*.5 + ticksHj*Math.Sin(alpha*Math.PI/180) }; Canvas.Children.Add(tick); tick.SetBinding(Shape.StrokeProperty, new Binding {Path = new PropertyPath(TicksForegroundProperty), Source = this}); tick.SetBinding(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath(TicksStrokeThicknessProperty), Source = this }); } for (var i = FromValue; i <= ToValue; i += LabelsStep) { var alpha = LinearInterpolation(fromAlpha, toAlpha, FromValue, ToValue, i) + 90; var tick = new Line { X1 = ActualWidth*.5 + ticksHi*Math.Cos(alpha*Math.PI/180), X2 = ActualWidth*.5 + labelsHj*Math.Cos(alpha*Math.PI/180), Y1 = ActualHeight*.5 + ticksHi*Math.Sin(alpha*Math.PI/180), Y2 = ActualHeight*.5 + labelsHj*Math.Sin(alpha*Math.PI/180) }; Canvas.Children.Add(tick); var label = new TextBlock { Text = LabelFormatter(i) }; label.SetBinding(EffectProperty, new Binding {Path = new PropertyPath(LabelsEffectProperty), Source = this}); Canvas.Children.Add(label); label.UpdateLayout(); Canvas.SetLeft(label, alpha < 270 ? tick.X2 : (Math.Abs(alpha - 270) < 4 ? tick.X2 - label.ActualWidth*.5 : tick.X2 - label.ActualWidth)); Canvas.SetTop(label, tick.Y2); tick.SetBinding(Shape.StrokeProperty, new Binding { Path = new PropertyPath(TicksForegroundProperty), Source = this }); tick.SetBinding(Shape.StrokeThicknessProperty, new Binding { Path = new PropertyPath(TicksStrokeThicknessProperty), Source = this }); } MoveStick(); }
TextBlock InsertLineCenteredText (Canvas FramingCanvas, String Text, double FontSizePercentage, double TopPercentage, String Weight, String Color) { FontWeightConverter FWConverter = new FontWeightConverter (); BrushConverter BRConverter = new BrushConverter (); TextBlock BlockName = new TextBlock (); BlockName.Visibility = Visibility.Visible; FramingCanvas.Children.Add (BlockName); FramingCanvas.UpdateLayout (); double ActCanvasHeight = FramingCanvas.ActualHeight; double ActCanvasWidth = FramingCanvas.ActualWidth; BlockName.FontSize = FontSizePercentage * ActCanvasHeight / 100; BlockName.FontWeight = (FontWeight)FWConverter.ConvertFromString (Weight); BlockName.Foreground = (Brush)BRConverter.ConvertFromString (Color); BlockName.Text = Text; BlockName.TextAlignment = TextAlignment.Center; BlockName.UpdateLayout (); double ActHeadHeight = BlockName.ActualHeight; double ActHeadWidth = BlockName.ActualWidth; Canvas.SetTop (BlockName, (ActCanvasHeight * TopPercentage) / 100); Canvas.SetLeft (BlockName, ((ActCanvasWidth - ActHeadWidth) / 2)); return BlockName; }
public TextBlock InsertCanvasPositionedTextBlock (Canvas FramingCanvas, String Text, double WidthPercentage, double HeightPercentage, double FontSizePercentage, double LeftPercentage, double TopPercentage, String Weight, String Color) { FontWeightConverter FWConverter = new FontWeightConverter (); BrushConverter BRConverter = new BrushConverter (); TextBlock BlockName = new TextBlock (); BlockName.Visibility = Visibility.Visible; FramingCanvas.Children.Add (BlockName); FramingCanvas.UpdateLayout (); double ActCanvasHeight = FramingCanvas.ActualHeight; if (ActCanvasHeight == 0) ActCanvasHeight = FramingCanvas.Height; double ActCanvasWidth = FramingCanvas.ActualWidth; if (ActCanvasWidth == 0) ActCanvasWidth = FramingCanvas.Width; Canvas.SetTop (BlockName, ((ActCanvasHeight * TopPercentage) / 100)); Canvas.SetLeft (BlockName, ((ActCanvasWidth * LeftPercentage) / 100)); double BlockNameWidth = (ActCanvasWidth * WidthPercentage) / 100; double BlockNameHeight = (ActCanvasHeight * HeightPercentage) / 100; Text = Text.Replace ("\r\n", " "); Text = Text.Replace ("\n", " "); Text = Text.Replace ("\r", " "); bool TextFit = false; double FontReduction = 1; while (!TextFit) { BlockName.FontSize = (ActCanvasHeight * FontSizePercentage / 100.0) * FontReduction; BlockName.FontWeight = (FontWeight)FWConverter.ConvertFromString (Weight); if (Color.IndexOf ('$') == -1) BlockName.Foreground = (Brush)BRConverter.ConvertFromString (Color); else BlockName.Foreground = (Brush)BRConverter.ConvertFromString (Color); BlockName.TextAlignment = TextAlignment.Left; BlockName.TextWrapping = TextWrapping.Wrap; BlockName.Text = Text; BlockName.UpdateLayout (); double ActTextHeight = BlockName.ActualHeight; double ActTextWidth = BlockName.ActualWidth; double NumberOfPossibleLines = (double)((int)(BlockNameHeight / ActTextHeight)); double RequiredNumberOfLines = (ActTextWidth * 1.10) / BlockNameWidth; if (RequiredNumberOfLines < NumberOfPossibleLines) { TextFit = true; } FontReduction -= 0.05; if (FontReduction < 0.30) TextFit = true; } BlockName.Width = BlockNameWidth; BlockName.Height = BlockNameHeight; return BlockName; }
TextBlock InsertCanvasPositionedText (Canvas FramingCanvas, String Text, double WidthPercentage, double HeightPercentage, double FontSizePercentage, String Weight, String Color) { FontWeightConverter FWConverter = new FontWeightConverter (); BrushConverter BRConverter = new BrushConverter (); TextBlock BlockName = new TextBlock (); BlockName.Visibility = Visibility.Visible; FramingCanvas.Children.Add (BlockName); double ActCanvasHeight = FramingCanvas.ActualHeight; double ActCanvasWidth = FramingCanvas.ActualWidth; BlockName.FontSize = ActCanvasHeight * FontSizePercentage / 100.0; BlockName.FontWeight = (FontWeight)FWConverter.ConvertFromString (Weight); BlockName.Foreground = (Brush)BRConverter.ConvertFromString (Color); BlockName.Text = Text; BlockName.TextAlignment = TextAlignment.Center; BlockName.UpdateLayout (); double ActTextHeight = BlockName.ActualHeight; double ActTextWidth = BlockName.ActualWidth; Canvas.SetTop (BlockName, (((ActCanvasHeight * HeightPercentage) / 100) - (ActTextHeight / 2))); Canvas.SetLeft (BlockName, (((ActCanvasWidth * WidthPercentage) / 100) - (ActTextWidth / 2))); return BlockName; }
private void SetContent(TextBlock textblock, string text) { textblock.Text = text; textblock.UpdateLayout(); }