Hi,@Pranav Kulkarni.
I encountered an error using your code and was unable to reproduce the problem.
I did the following workaround, so you could see if it helps you.
I scaled by setting the StrokeThickness of the path relative to.
<Window.Resources>
<local:RectangleConverter x:Key="rectConverter"/>
</Window.Resources>
<Grid x:Name="mainGrid" MouseWheel="Canvas_MouseWheel" Width="{Binding Width}" Height="{Binding Height}">
<Grid.Resources>
<ScaleTransform x:Key="scale" ScaleX="1.5" ScaleY="1.5"/>
</Grid.Resources>
<Path Fill="blue" Stroke="red" StrokeThickness="1" x:Name="path" RenderTransform="{StaticResource ResourceKey=scale}">
<Path.Data>
<RectangleGeometry>
<RectangleGeometry.Rect>
<MultiBinding Converter="{StaticResource rectConverter}">
<Binding Path="Width" ElementName="mainGrid" />
<Binding Path="Height" ElementName="mainGrid"/>
</MultiBinding>
</RectangleGeometry.Rect>
</RectangleGeometry>
</Path.Data>
</Path>
</Grid>
public partial class MainWindow : System.Windows.Window
{
public double Width { get; set; } = 10;
public double Height { get; set; } = 10;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private Double zoomMax = 5;
private Double zoomMin = 0.5;
private Double zoomSpeed = 0.001;
private Double zoom = 1;
private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
{
var transform = mainGrid.Resources["scale"] as ScaleTransform;
zoom += zoomSpeed * e.Delta;
if (zoom < zoomMin) { zoom = zoomMin; }
System.Windows.Point mousePos = e.GetPosition(mainGrid);
if (zoom > 1)
{
mainGrid.RenderTransform = new ScaleTransform(zoom, zoom, mousePos.X, mousePos.Y);
}
else
{
mainGrid.RenderTransform = new ScaleTransform(zoom, zoom);
}
path.StrokeThickness = 1.0 / zoom;
}
}
public class RectangleConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return new Rect(100, 100, (double)values[0]/5, (double)values[1]/5 );
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
The result:
-
If the response is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.