【发布时间】:2026-01-09 00:15:01
【问题描述】:
所以,我有一个注册表格,在设计中,它看起来像这样:
这里是 XAML:
<Rectangle x:Name="signInBox" Fill="#FF5F5F6E" HorizontalAlignment="Center" Height="450" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="640" StrokeThickness="3" Visibility="Visible"/>
<TextBlock x:Name="signUpText" HorizontalAlignment="Center" Margin="0,-350,0,0" TextWrapping="Wrap" Text="Sign up for Horizon Chat" VerticalAlignment="Center" FontFamily="Muli" FontSize="36" Foreground="White"/>
<!-- For the username box -->
<TextBox x:Name="nameSignUpBox" HorizontalAlignment="Center" Margin="0,-150,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="370" FontFamily="Muli" FontSize="26" BorderThickness="2" MaxHeight="61" MaxWidth="686"/>
<TextBlock x:Name="nameSignUpBoxDescription" HorizontalAlignment="Center" Margin="456,334,738,544" TextWrapping="Wrap" Text="Username" VerticalAlignment="Center" FontFamily="Muli" FontSize="18" Foreground="#FFDADADA"/>
<!-- For the email box -->
<TextBox x:Name="emailSignUpBox" HorizontalAlignment="Center" Margin="0,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="370" FontFamily="Muli" FontSize="26" BorderThickness="2" MaxHeight="61" MaxWidth="686"/>
<TextBlock x:Name="emailSignUpBoxDescription" HorizontalAlignment="Center" Margin="456,408,778,468" TextWrapping="Wrap" Text="Email" VerticalAlignment="Center" FontFamily="Muli" FontSize="18" Foreground="#FFDADADA"/>
<!-- For the password box -->
<PasswordBox x:Name="passwordSignUpBox" HorizontalAlignment="Center" Margin="0,150,0,0" VerticalAlignment="Center" Width="370" FontFamily="Muli" FontSize="26" BorderThickness="2" MaxHeight="61" MaxWidth="686"/>
<TextBlock x:Name="passwordSignUpBoxDescription" HorizontalAlignment="Center" Margin="455,484,742,393" TextWrapping="Wrap" Text="Password" VerticalAlignment="Center" FontFamily="Muli" FontSize="18" Foreground="#FFDADADA"/>
然后,当程序启动时,它会进行一些更改,例如更改窗口大小以适合该框,并使其可见,等等。这是代码:
BrushConverter brushconverter = new BrushConverter();
this.Background = (Brush)brushconverter.ConvertFrom("#FF9C9C9C");
this.Width = 640;
this.Height = 450;
this.ResizeMode = ResizeMode.NoResize;
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double windowWidth = this.Width;
double windowHeight = this.Height;
this.Left = (screenWidth / 2) - (windowWidth / 2);
this.Top = (screenHeight / 2) - (windowHeight / 2);
现在,所有这些都与窗口有关,因为在我的例子中,我已经看到了所有的东西。因此,当我启动程序时,我会得到所有除了框上方的小文本(用户名、电子邮件和密码)。是这样的:
现在,我的猜测是它在调整大小期间将文本移动到某处,因为当我不调整窗口大小时,它仍然存在(就像在设计器中一样)。但是,如果将我的文本移动到不同的地方,这仍然是一个问题。我该如何解决这个问题?
编辑:我尝试调整设计器窗口的大小,这是将其调整为 100 像素左右的前后:
之前:
50%:
之后:
【问题讨论】:
-
您真的不应该使用边距来放置文本,如果您需要这样做,请使用画布而不是网格。但是,您真正应该做的是使用行/列在网格中正确布置标签/文本框,这些行/列将在没有代码隐藏的情况下调整大小。
-
@RonBeyer 在文本上使用边距有什么缺点?
-
正是您所经历的! WPF 被制作为“独立于分辨率”,您尝试做的是使用像素偏移然后缩放它们,基本上将 WinForms 实践硬塞到 WPF 中。如果您正确设置网格并将文本放置在正确的位置,您不必担心代码隐藏来缩放表单,WPF 会为您完成。
-
如果您懒得重新设计您的 XAML,尽管对于此类事情来说这是一种不好的做法,但有一条线可以解决您的问题 - 将所有内容放在 ViewBox 中。