【问题标题】:Removing all position constraints for a XAML element删除 XAML 元素的所有位置约束
【发布时间】:2019-05-23 14:39:26
【问题描述】:

我正在构建一个 UWP 应用。我正在使用一个 Rectangle XAML 元素,我在 XAML 代码中为其指定了一个定位属性-RelativePanel.RightOf()。现在单击特定按钮时,我需要将 Rectangle 从其原始位置移动到其他控件的左侧。所以在接收到 Click 事件时,我使用属性 RelativePanel.SetLeftOf() 来移动它。

XAML 代码-

    <StackPanel>
        <RelativePanel Height="50" Width="200">
            <Rectangle x:Name="_redRect" Height="50" Width="50" Fill="Red" RelativePanel.AlignLeftWithPanel="True"/>
            <Rectangle x:Name="_blueRect" Height="50" Width="50" Fill="Blue" RelativePanel.RightOf="_redRect"/>
            <Rectangle x:Name="_greenRect" Height="50" Width="50" Fill="Green" RelativePanel.AlignRightWithPanel="True"/>
        </RelativePanel>
        <Button x:Name="_switch" Click="OnSwitchClicked">Switch</Button>
    </StackPanel>

C#代码-

    private void OnSwitchClicked(object sender, RoutedEventArgs e)
    {
        RelativePanel.SetLeftOf(_blueRect, _greenRect);
    }

但这并不能解决我的问题。 _blueRect 似乎位于 _redRect 和 _greenRect 的中间。这可能是因为第一个约束 RelativePanel.LeftOf = "_redRect" 尚未删除。如何通过代码移除该约束?

还有没有什么方法可以一起去掉所有的定位约束?

【问题讨论】:

  • 展示和讲述。向我们展示您尝试过的内容(意味着您已经努力实际编写您需要的代码),具体来说是遇到了什么。 ...而且...我们会告诉答案:)
  • @Epistaxis 完成

标签: c# xaml uwp


【解决方案1】:

这可能是因为第一个约束 RelativePanel.LeftOf = "_redRect" 没有被删除。如何通过代码移除该约束?

如果您想删除RelativePanel.RightOf="_redRect",您可以使用相同的方式在代码中将RelativePanel.RightOf 设置为null。

private void OnSwitchClicked(object sender, RoutedEventArgs e)
{
    RelativePanel.SetLeftOf(_blueRect, _greenRect);
    RelativePanel.SetRightOf(_blueRect, null);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 2015-07-03
    • 2022-01-26
    • 2011-02-16
    • 2011-04-10
    • 1970-01-01
    相关资源
    最近更新 更多