英文:
What is the problem with zooming the window causing the rectangle to disappear?
问题
当使用 WPF 缩放窗口时,矩形的一部分已经消失
在缩放时,我应该如何使窗口正确显示矩形?
<Grid Background="Azure">
<Grid x:Name="Grid01" HorizontalAlignment="Left" Background="AliceBlue">
<Image x:Name="ShowImage" HorizontalAlignment="Left"/>
<Rectangle x:Name="myRectangle"/>
</Grid>
</Grid>
我的后端代码
myRectangle.Fill = null;
myRectangle.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
myRectangle.StrokeThickness = 3;
myRectangle.Width = nowWidth;
myRectangle.Height = nowHeight;
myRectangle.Margin = new Thickness(nowX, nowY - nowWidth / 2, 0, 0);
System.Windows.Media.RotateTransform rotateTransform = new(angle);
myRectangle.RenderTransform = rotateTransform;
myRectangle.RenderTransformOrigin = new System.Windows.Point(0, 0);//渲染动画的起点-取值范围0-1
英文:
When scaling the window with wpf, Part of the rectangle has disappeared
How should I make the window display rectangles properly when zooming?
<Grid Background="Azure">
<Grid x:Name="Grid01" HorizontalAlignment="Left" Background="AliceBlue">
<Image x:Name="ShowImage" HorizontalAlignment="Left"/>
<Rectangle x:Name="myRectangle"/>
</Grid>
</Grid>
My backend code
myRectangle.Fill = null;
myRectangle.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
myRectangle.StrokeThickness = 3;
myRectangle.Width = nowWidth;
myRectangle.Height = nowHeight;
myRectangle.Margin = new Thickness(nowX, nowY - nowWidth / 2, 0, 0);
System.Windows.Media.RotateTransform rotateTransform = new(angle);
myRectangle.RenderTransform = rotateTransform;
myRectangle.RenderTransformOrigin = new System.Windows.Point(0, 0);//渲染动画的起点-取值范围0-1
答案1
得分: 0
如果您将所有内容包装在一个视图框(Viewbox)中,那么这两个控件将会相互缩放:
<Grid Background="Azure">
<Viewbox>
<Grid x:Name="Grid01" HorizontalAlignment="Left" Width="1920" Height="1080" Background="HotPink">
<Image x:Name="ShowImage"/>
<Rectangle x:Name="myRectangle" Height="100" Width="100" Stroke="Red" StrokeThickness="5" Margin="10,10,0,0"/>
</Grid>
</Viewbox>
</Grid>
英文:
if you wrap it all in a viewbox you the two controls wil scale with each other:
<Grid Background="Azure">
<Viewbox>
<Grid x:Name="Grid01" HorizontalAlignment="Left" Width="1920" Height="1080" Background="HotPink">
<Image x:Name="ShowImage"/>
<Rectangle x:Name="myRectangle" Height="100" Width="100" Stroke="Red" StrokeThickness="5" Margin="10,10,0,0"/>
</Grid>
</Viewbox>
</Grid>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论