【问题标题】:How do i align an Element in UWP that i generate through code?如何对齐通过代码生成的 UWP 中的元素?
【发布时间】:2020-12-22 10:25:41
【问题描述】:

我正在编写我的第一个 UWP 应用程序,我遇到的问题是我想从 C# 代码生成按钮,以便在需要时动态生成它们。在 Xaml 代码中,对齐是通过以下方式完成的: RelativePanel.AlignLeftWithPanel="True"

(<Button x:Name="Button1" Content="Button" FontSize="50" FontFamily="Muli" RelativePanel.AlignLeftWithPanel="True" Height="250" Width="450" Margin="30" Background="#9cccd2"/>).

现在,当我尝试使用 Code 时,有 Button.HorizontalAlignment = HorizontalAlignment.Right; 选项。但这并没有将我的按钮与相关面板的右侧对齐,它似乎没有任何改变,按钮仍然出现在左侧。

有没有办法通过代码对齐我的按钮而无需指定确切的位置?

提前感谢您的帮助。

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    如何对齐通过代码生成的 UWP 中的元素?

    您可以在后面的代码中调用RelativePanel.SetAlignRightWithPanel 方法,如下所示。

    RelativePanel.SetAlignRightWithPanel(Button1, true);
    

    现在当我尝试使用代码时,有 Button.Horizo​​ntalAlignment = Horizo​​ntalAlignment.Right;选项。但这不会使我的按钮与相关面板的右侧对齐

    UI 元素上的 Horizo​​ntalAlignment 和 VerticalAlignment 属性是在评估和应用关系属性之后应用的。如果所需尺寸小于可用尺寸,这些属性控制元素在可用尺寸内的放置。更多信息请参考document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-05
      • 2016-04-14
      • 1970-01-01
      • 2011-06-04
      • 2020-10-19
      • 2019-10-17
      • 1970-01-01
      • 2010-09-05
      相关资源
      最近更新 更多