Amro Samy Amro Samy - 1 year ago 168
C# Question

Canvas RuleLines With grid UWP

I'm working in a program which contains a canvas ,
This canvas includes some shapes and RuleLines ,
I added a grid to the canvas and it looked like this
enter image description here

the problem is the program is not smooth when I Manipulate or Zoom this canvas,

this is the Grid code in C# :

public void GridPartitions (Grid grid)
for (int ii = 0; ii < 50; ii++)
MyGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) });
MyGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(20) });
for (int row = 0; row < 50; row++)
for(int coulmn = 0; coulmn < 50; coulmn++)
var PartitionRectangle = new Rectangle();
PartitionRectangle.Stroke = new SolidColorBrush() { Color = Color.FromArgb(255, 0, 0, 0) };
PartitionRectangle.StrokeThickness = 0.2;
Grid.SetRow(PartitionRectangle, row);
Grid.SetColumn(PartitionRectangle, coulmn);

However in Microsoft OneNote UWP
it has this option and it runs very smooth whatever the the number of the RuleLines and looks like this :
enter image description here

Any ideas to Improve My Code ?

Thank You.

Answer Source

For smooth zooming and Support gestures and (Ctrl + MouseWheel) you can put the canvas inside ScrollViewer. Here is example Code:

<ScrollViewer ZoomMode="Enabled" ZoomSnapPointsType="Mandatory"> 
     <Canvas Background="White" RenderTransformOrigin="0.5,0.5" 
            ManipulationDelta="MyCanvas_ManipulationDelta" ManipulationMode="All" CacheMode="BitmapCache">   

        <Rectangle Width="500" Height="500" Fill="Black"/>

actually its very smooth Now I'm working on adding adaptive Grid

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download