Drawing Grid on Canvas and Retaining during Navigation

BigH61 581 Reputation points
2023-01-22T07:15:27.6766667+00:00

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.

text

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,705 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2024-01-22T12:22:45.82+00:00

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

    <UserControl x:Class="NavigationTest.Views.OneView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:vm="clr-namespace:NavigationTest.ViewModels"
                 xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
                 xmlns:bh="clr-namespace:NavigationTest.Behaviors"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
    
        <!--<UserControl.DataContext>
            <vm:OneViewModel/>
        </UserControl.DataContext>-->
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="52"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <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"/>
            </StackPanel>
            <Grid Grid.Row="1">
                <Canvas x:Name="BackgroundCanvas" Width="800" Height="450">
                    <behaviors:Interaction.Behaviors>
                        <bh:BackgroundCanvasBehavior/>
                    </behaviors:Interaction.Behaviors>
                </Canvas>
            </Grid>
        </Grid>
    </UserControl>
    
    
    

    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;
    			set
    			{
    				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()
    		{
    			BackgroundCanvas.Children.Clear();
    			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