DrawingHost.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Media;
  7. namespace MuchInfo.Chart.WPF.Primitives.Drawing
  8. {
  9. public class DrawingHost : UIElement
  10. {
  11. private VisualCollection _children;
  12. protected override int VisualChildrenCount
  13. {
  14. get { return _children.Count; }
  15. }
  16. public DrawingHost()
  17. {
  18. _children=new VisualCollection(this);
  19. }
  20. // Provide a required override for the GetVisualChild method.
  21. protected override Visual GetVisualChild(int index)
  22. {
  23. if (index < 0 || index >= _children.Count)
  24. {
  25. throw new ArgumentOutOfRangeException();
  26. }
  27. return _children[index];
  28. }
  29. public void ClearDrawing()
  30. {
  31. _children.Clear();
  32. if (_children != null )
  33. foreach (var child in _children)
  34. {
  35. this.RemoveVisualChild(child);
  36. }
  37. }
  38. public void DrawingLine(Brush penBrush, double thickness, Point point1, Point point2)
  39. {
  40. var visual=new DrawingVisual();
  41. var drawContext= visual.RenderOpen();
  42. drawContext.DrawLine(new Pen(penBrush,thickness),point1,point2);
  43. drawContext.Close();
  44. _children.Add(visual);
  45. }
  46. }
  47. }