Hello! The task has been set: there is a graphical editor of the schemes, it is necessary to draw the grid and attach it (well, that is, the coordinates of the elements are adjusted to the grid when dragging).
There is a class SizedCanvas , which is inherited from Canvas . The main workspace is the canvasWorkspace object of type SizedCanvas .
I implement rendering using a class that inherits from Adorner and overloads the onRender method. So far I have done this, but the cycle generally needs to be replaced.
class LineGridAdorner : Adorner { public int step { get; set; } //Шаг сетки public double width { get; set; } //Ширина поля public double height { get; set; } //Высота поля private Pen pen = new Pen(Brushes.Coral, 0.2); public LineGridAdorner(UIElement adornedElement) : base(adornedElement) { step = 50; pen.DashStyle = DashStyles.Dash; } protected override void OnRender(DrawingContext drawingContext) { for (int x = 1; x < 100; x++) { drawingContext.DrawLine(pen, new Point(x * step, 0), new Point(x * step, 1000)); drawingContext.DrawLine(pen, new Point(0, x * step), new Point(2000, x * step)); } } } Here is a piece of code that gets called after all the elements on the canvas are rendered:
private void canvasWorkspace_Loaded(object sender, RoutedEventArgs e) { lineGrid = new LineGridAdorner(canvasWorkspace); lineGrid.height = canvasWorkspace.ActualHeight; lineGrid.width = canvasWorkspace.ActualWidth; lineGrid.step = 50; layer = AdornerLayer.GetAdornerLayer(canvasWorkspace); layer.Add(lineGrid); isGridShown = true; } Actually, the render is almost done, and the binding is almost finished. The problem is that it is very lagging. Those. when frames are drawn (and this happens both when the object is moved and when it is selected), a cycle is triggered, which is roughly performed 100 times, and draws 2 lines per cycle. It is expensive, lagging and not suitable. How can you optimize all this? Theoretically, if I knew the range of coordinates (the top element is ScrollerView , the Content field of which is canvasWorkspace ) that are currently visible, I could think of something.