我正在尝试在Windows通用应用程序上生成动态表单并且边缘有问题。
我喜欢将Radiobutton和Textbox保留在边缘0的左侧。 问题是文本框不能留在左边,我有两个控件,边距保持为0,不知何故文本框正在推到屏幕右侧。
这是我运行应用程序后的结果。
Windows 10上的Windows Universal应用程序的输出结果
我的脚本文件名MainPage.xaml.cs,用于项目AppTestTrash
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace AppTestTrash
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
//Add dynamic RadioButton
var dynamicRadiobtn = new RadioButton();
dynamicRadiobtn.Name = "testme";
dynamicRadiobtn.Height = 30;
dynamicRadiobtn.Width = 50;
dynamicRadiobtn.Margin = new Thickness(0, 100, 0, 0);
dynamicForm.Children.Add(dynamicRadiobtn);
//Add dynamic texbox
var dynamicTextbox = new TextBox();
dynamicTextbox.Name = "testme2";
dynamicTextbox.Height = 30;
dynamicTextbox.Width = 50;
dynamicTextbox.Margin = new Thickness(0, 120, 0, 0);
dynamicForm.Children.Add(dynamicTextbox);
}
}
}
XAML文件mainpage.xaml
<Page
x:Class="AppTestTrash.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppTestTrash"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded">
<Grid Name="mainGrid" BorderBrush="AliceBlue" Width="500" Height="500" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" BorderThickness="2">
<StackPanel Name="dynamicForm" BorderThickness="1">
</StackPanel>
</Grid>
</Page>
答案 0 :(得分:2)
要解决此问题,您可以将dynamicTextbox
的{{3}}属性设置为HorizontalAlignment,如下所示。
//Add dynamic texbox
var dynamicTextbox = new TextBox();
dynamicTextbox.Name = "testme2";
dynamicTextbox.Height = 30;
dynamicTextbox.Width = 50;
dynamicTextbox.HorizontalAlignment = HorizontalAlignment.Left;
dynamicTextbox.Margin = new Thickness(0, 120, 0, 0);
dynamicForm.Children.Add(dynamicTextbox);
HorizontalAlignment
的默认值为拉伸,因此即使左边距为0,您的文本框也会位于网格的中心。有关详细信息,请参阅备注 Left。