如何在不破坏相对关系的情况下任意定位 RelativePanel 中的元素?
How To Arbitrarily Position Element in a RelativePanel Without Breaking Relative Relationships?
我在特定的 X、Y 坐标处有一个红色矩形,例如。 (510, 280)。我希望蓝色矩形对接在它的左侧。所以我试试这个:
<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>
但是RelativePanel不识别RedShape的位置。 Redshape 位于 UI 中的 (510, 280),但 BlueShape 的位置就像 RedShape 仍在 (0, 0) 处一样。
我尝试使用视觉层重新定位 RedShape,而不是 RenderTransform。
var visual = ElementCompositionPreview.GetElementVisual(RedShape);
visual.Offset = new System.Numerics.Vector3(510f, 280f, 0);
同样的问题,红色去了它应该去的地方,但蓝色仍然认为它在 (0, 0)。如果我使用 RelativePanel 约束定位 RedShape,那么一切都会按预期进行。但这限制了我将它定位到面板的中心或边缘。
在计算布局后应用转换,这就是第一个控件(根据布局)的位置仍为 (0,0) 的原因。见题目Transforms and layout.
遗憾的是,RelativePanel 不支持任意定位内容,Canvas 支持任意定位内容,但不支持相对关系。您可能需要为您的方案使用面板组合。
我在特定的 X、Y 坐标处有一个红色矩形,例如。 (510, 280)。我希望蓝色矩形对接在它的左侧。所以我试试这个:
<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>
但是RelativePanel不识别RedShape的位置。 Redshape 位于 UI 中的 (510, 280),但 BlueShape 的位置就像 RedShape 仍在 (0, 0) 处一样。
我尝试使用视觉层重新定位 RedShape,而不是 RenderTransform。
var visual = ElementCompositionPreview.GetElementVisual(RedShape);
visual.Offset = new System.Numerics.Vector3(510f, 280f, 0);
同样的问题,红色去了它应该去的地方,但蓝色仍然认为它在 (0, 0)。如果我使用 RelativePanel 约束定位 RedShape,那么一切都会按预期进行。但这限制了我将它定位到面板的中心或边缘。
在计算布局后应用转换,这就是第一个控件(根据布局)的位置仍为 (0,0) 的原因。见题目Transforms and layout.
遗憾的是,RelativePanel 不支持任意定位内容,Canvas 支持任意定位内容,但不支持相对关系。您可能需要为您的方案使用面板组合。