简体   繁体   中英

How do i align an Element in UWP that i generate through code?

I am Programming my first UWP application and i have the problem that i want to generate Buttons from C# code so i can generate them dynamically when needed. In Xaml code the alignment is done through: RelativePanel.AlignLeftWithPanel="True"

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

Now when i try it with Code there is the Button.HorizontalAlignment = HorizontalAlignment.Right; option. But that does not align my button to the right of the relative Panel, it seemingly changes nothing at all, the Button still appears on the left side.

Is there a way to align my button via Code without having to specify the exact position?

Thanks for your help in advance.

How do i align an Element in UWP that i generate through code?

You could call RelativePanel.SetAlignRightWithPanel method in the code behind like the following.

RelativePanel.SetAlignRightWithPanel(Button1, true);

Now when i try it with Code there is the Button.HorizontalAlignment = HorizontalAlignment.Right; option. But that does not align my button to the right of the relative Panel

The HorizontalAlignment and VerticalAlignment properties on UI elements are applied after relationship properties are evaluated and applied. These properties control the placement of the element within the available size for the element, if the desired size is smaller than the available size. For more please refer this document .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM