放大窗口会导致矩形消失的问题是什么?

huangapple go评论72阅读模式
英文:

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?

&lt;Grid Background=&quot;Azure&quot;&gt;
    &lt;Grid x:Name=&quot;Grid01&quot; HorizontalAlignment=&quot;Left&quot;  Background=&quot;AliceBlue&quot;&gt;
        &lt;Image x:Name=&quot;ShowImage&quot;  HorizontalAlignment=&quot;Left&quot;/&gt;
        &lt;Rectangle x:Name=&quot;myRectangle&quot;/&gt;
    &lt;/Grid&gt;
&lt;/Grid&gt;

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:

                &lt;Grid Background=&quot;Azure&quot;&gt;
                    &lt;Viewbox&gt;                            
                        &lt;Grid x:Name=&quot;Grid01&quot; HorizontalAlignment=&quot;Left&quot;  Width=&quot;1920&quot; Height=&quot;1080&quot; Background=&quot;HotPink&quot;&gt;
                            &lt;Image x:Name=&quot;ShowImage&quot;/&gt;
                            &lt;Rectangle x:Name=&quot;myRectangle&quot; Height=&quot;100&quot; Width=&quot;100&quot; Stroke=&quot;Red&quot; StrokeThickness=&quot;5&quot; Margin=&quot;10,10,0,0&quot;/&gt;
                        &lt;/Grid&gt;
                    &lt;/Viewbox&gt;
                &lt;/Grid&gt;

huangapple
  • 本文由 发表于 2023年6月1日 15:10:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定