TestScrollable.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using Perspex;
  3. using Perspex.Controls;
  4. using Perspex.Controls.Primitives;
  5. using Perspex.Media;
  6. namespace XamlTestApplication
  7. {
  8. public class TestScrollable : Control, IScrollable
  9. {
  10. private int itemCount = 100;
  11. private Size _extent;
  12. private Vector _offset;
  13. private Size _viewport;
  14. private Size _lineSize;
  15. public Action InvalidateScroll { get; set; }
  16. Size IScrollable.Extent
  17. {
  18. get { return _extent; }
  19. }
  20. Vector IScrollable.Offset
  21. {
  22. get { return _offset; }
  23. set
  24. {
  25. _offset = value;
  26. InvalidateVisual();
  27. }
  28. }
  29. Size IScrollable.Viewport
  30. {
  31. get { return _viewport; }
  32. }
  33. protected override Size MeasureOverride(Size availableSize)
  34. {
  35. using (var line = new FormattedText(
  36. "Item 100",
  37. TextBlock.GetFontFamily(this),
  38. TextBlock.GetFontSize(this),
  39. TextBlock.GetFontStyle(this),
  40. TextAlignment.Left,
  41. TextBlock.GetFontWeight(this)))
  42. {
  43. line.Constraint = availableSize;
  44. _lineSize = line.Measure();
  45. return new Size(_lineSize.Width, _lineSize.Height * itemCount);
  46. }
  47. }
  48. protected override Size ArrangeOverride(Size finalSize)
  49. {
  50. _viewport = new Size(finalSize.Width, finalSize.Height / _lineSize.Height);
  51. _extent = new Size(_lineSize.Width, itemCount + 1);
  52. InvalidateScroll?.Invoke();
  53. return finalSize;
  54. }
  55. public override void Render(DrawingContext context)
  56. {
  57. var y = 0.0;
  58. for (var i = (int)_offset.Y; i < itemCount; ++i)
  59. {
  60. using (var line = new FormattedText(
  61. "Item " + (i + 1),
  62. TextBlock.GetFontFamily(this),
  63. TextBlock.GetFontSize(this),
  64. TextBlock.GetFontStyle(this),
  65. TextAlignment.Left,
  66. TextBlock.GetFontWeight(this)))
  67. {
  68. context.DrawText(Brushes.Black, new Point(-_offset.X, y), line);
  69. y += _lineSize.Height;
  70. }
  71. }
  72. }
  73. }
  74. }