Drawing Grid on Canvas and Retaining during Navigation

BigH61 581 Reputation points

I hope some one can help.

I am working on a project were I can draw a grid on a Canvas but when you navigate away then back the grid has been removed.

The navigation process does not result in new views.

How can I retain the grid between navigation or at least redraw when navigating back. The drawing process relies upon Behaviors therefore I cannot use such Events as Loaded as the Canvas Behavior reference is null.

I have included a link to a demonstration project.


Thankyou for your assistance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,634 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,056 Reputation points

    one of possible solution can be this: XAML of OneView without UserControl.DataContext:

    <UserControl x:Class="NavigationTest.Views.OneView"
                 d:DesignHeight="450" d:DesignWidth="800">
                <RowDefinition Height="52"/>
            <StackPanel Orientation="Horizontal">
                <Label Grid.Row="0" FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center" Content="Home"/>
                <CheckBox Content="Draw Grid" Margin="10,5,10,5" IsChecked="{Binding DrawGrid}" VerticalAlignment="Center"/>
                <TextBlock Text="Grid Spacing" VerticalAlignment="Center" Margin="10"/>
                <TextBox Width="50" Text="{Binding GridSpacing}" Margin="10"/>
            <Grid Grid.Row="1">
                <Canvas x:Name="BackgroundCanvas" Width="800" Height="450">

    An changed code for OneViewModel:

    using NavigationTest.Helpers;
    using System.Windows.Controls;
    using System.Windows.Media;
    namespace NavigationTest.ViewModels
    	public class OneViewModel : BaseViewModel
    		private Canvas _backgroundCanvas;
    		public Canvas BackgroundCanvas
    			get => this._backgroundCanvas;
    				this._backgroundCanvas = value;
    				if (_DrawGrid) GoDrawGrid();
    		private bool _DrawGrid;
    		public bool DrawGrid
    			get { return _DrawGrid; }
    			set { if (_DrawGrid != value) _DrawGrid = value; OnPropertyChanged(nameof(DrawGrid)); GoDrawGrid(); }
    		private int _GridSpacing = 20;
    		public int GridSpacing
    			get { return _GridSpacing; }
    			set { if (_GridSpacing != value) _GridSpacing = value; OnPropertyChanged(nameof(GridSpacing)); }
    		public OneViewModel()
    		private void GoDrawGrid()
    			if (DrawGrid == true)
    				SolidColorBrush gridColorBrush = new SolidColorBrush(Colors.Red);
    				int SelectedScale = 1;//Temp
    				bool AutoScaleGrid = true;//Temp
    				ShowGrid showGrid = new ShowGrid(BackgroundCanvas, gridColorBrush);
    				showGrid.CreateGrid(GridSpacing, SelectedScale, AutoScaleGrid);
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful