代码之家  ›  专栏  ›  技术社区  ›  Sean O'Neil

如何在相对面板中任意定位元素而不破坏相对关系?

  •  0
  • Sean O'Neil  · 技术社区  · 6 年前

    我有一个红色的矩形,我想要在一个特定的x,y坐标,例如(510280)。我要蓝色的矩形对接到它的左边。所以我试试这个:

    <RelativePanel>
        <Rectangle x:Name="RedShape"  Fill="Red"  Width="400" Height="300">
            <Rectangle.RenderTransform>
                  <TranslateTransform X="510" Y="280" />
            </Rectangle.RenderTransform>
        </Rectangle>
        <Rectangle x:Name="BlueShape" Fill="Blue" Width="200" Height="100" RelativePanel.LeftOf="RedShape" />
    </RelativePanel>
    

    但是,相对论小组不承认红色形状的位置。在用户界面中,红色图形位于(510280),但蓝色图形的位置好像红色图形仍位于(0,0)。

    我尝试用视觉层重新定位红色形状,而不是rendertransform。

    var visual = ElementCompositionPreview.GetElementVisual(RedShape);
    visual.Offset = new System.Numerics.Vector3(510f, 280f, 0);
    

    同样的问题,红色会去到它应该去的地方,但是蓝色仍然认为它在(0,0)。如果我使用relativepanel约束来定位redshape,那么一切都将按预期工作。但这限制了我将其定位到面板的中心或边缘。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Breeze Liu - MSFT    6 年前