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);
}
}
}
}