Window 打开时文本消失
Text Disappearing When Window Opens
所以,我有一个注册表格,在设计中,它看起来像这样:
这里是 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"/>
然后,当程序启动时,它会进行一些更改,例如更改 window 大小以适合该框,并使其可见,等等。这是相关代码:
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);
现在,所有这些都与 window 有关,因为在我的情况下,我已经拥有所有可见的东西。因此,当我启动该程序时,除了 框上方的小文本(用户名、电子邮件和密码),我得到了所有内容 。这是:
现在,我的猜测是它在调整大小时将文本移动到某处,因为当我不调整 window 大小时,它仍然存在(就像在设计器中一样)。但是,如果它将我的文本移动到不同的地方,这仍然是一个问题。我该如何解决这个问题?
编辑: 我尝试调整设计器的大小 window,这是调整大小大约 100px 的前后:
之前:
50%:
之后:
我使用 @shadow32 将控件重新组织到一个视图框中,并为您的动态调整大小要求创建了一个纯粹的 xaml 解决方案。
我也清理了XAML,少即是多呵呵。
希望您可以使用这种方法,尽可能在设计器中保留布局。
它提供了一个更易于维护的解决方案。
<Window x:Class="WpfApp1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Window.Resources>
<!--SET THE TEXT STYLING, AVOID REPEATING THE SAME STYLE-->
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Muli" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="Foreground" Value="#FFDADADA"/>
<!--MARGIN SPACES OUT THE DATA ENTRY FIELDS-->
<Setter Property="Margin" Value="0,5,0,0"></Setter>
</Style>
</Window.Resources>
<!--ROOT CONTAINER CONTROL WITH MARGIN TO CENTER-->
<DockPanel Margin="50,0" >
<!--SIDE BORDER AND BACKGROUND-->
<Border BorderBrush="Black" BorderThickness="1,0" Background="#FF5F5F6E">
<!--VIEWBOX FOR SCALING-->
<Viewbox VerticalAlignment="Top">
<!--DOCK PANEL FOR WRAPPING HEADER AND DATA ENTRY CONTROLS-->
<DockPanel >
<!--HEADER WITH MARGIN TO PAD THE TOP-->
<TextBlock x:Name="signUpText"
Text="Sign up for Horizon Chat"
Foreground="White"
Margin="0,10,0,0"
DockPanel.Dock="Top"
FontSize="20"/>
<!--STACKPANEL FOR ORDERING CONTROLS-->
<StackPanel DockPanel.Dock="Bottom" Margin="5,0">
<!--TOP PADDING TEXTBLOCK-->
<TextBlock />
<!-- For the username box -->
<TextBlock x:Name="nameSignUpBoxDescription"
Text="Username" />
<TextBox x:Name="nameSignUpBox" />
<!-- For the email box -->
<TextBlock x:Name="emailSignUpBoxDescription"
Text="Email" />
<TextBox x:Name="emailSignUpBox" />
<!-- For the password box -->
<TextBlock x:Name="passwordSignUpBoxDescription"
Text="Password" />
<PasswordBox x:Name="passwordSignUpBox" />
<!--BOTTOM PADDING TEXTBLOCK-->
<TextBlock />
</StackPanel>
</DockPanel>
</Viewbox>
</Border>
</DockPanel>
</Window>
所以,我有一个注册表格,在设计中,它看起来像这样:
这里是 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"/>
然后,当程序启动时,它会进行一些更改,例如更改 window 大小以适合该框,并使其可见,等等。这是相关代码:
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);
现在,所有这些都与 window 有关,因为在我的情况下,我已经拥有所有可见的东西。因此,当我启动该程序时,除了 框上方的小文本(用户名、电子邮件和密码),我得到了所有内容 。这是:
现在,我的猜测是它在调整大小时将文本移动到某处,因为当我不调整 window 大小时,它仍然存在(就像在设计器中一样)。但是,如果它将我的文本移动到不同的地方,这仍然是一个问题。我该如何解决这个问题?
编辑: 我尝试调整设计器的大小 window,这是调整大小大约 100px 的前后:
之前:
50%:
之后:
我使用 @shadow32 将控件重新组织到一个视图框中,并为您的动态调整大小要求创建了一个纯粹的 xaml 解决方案。
我也清理了XAML,少即是多呵呵。
希望您可以使用这种方法,尽可能在设计器中保留布局。
它提供了一个更易于维护的解决方案。
<Window x:Class="WpfApp1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Window.Resources>
<!--SET THE TEXT STYLING, AVOID REPEATING THE SAME STYLE-->
<Style TargetType="TextBlock">
<Setter Property="FontFamily" Value="Muli" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="Foreground" Value="#FFDADADA"/>
<!--MARGIN SPACES OUT THE DATA ENTRY FIELDS-->
<Setter Property="Margin" Value="0,5,0,0"></Setter>
</Style>
</Window.Resources>
<!--ROOT CONTAINER CONTROL WITH MARGIN TO CENTER-->
<DockPanel Margin="50,0" >
<!--SIDE BORDER AND BACKGROUND-->
<Border BorderBrush="Black" BorderThickness="1,0" Background="#FF5F5F6E">
<!--VIEWBOX FOR SCALING-->
<Viewbox VerticalAlignment="Top">
<!--DOCK PANEL FOR WRAPPING HEADER AND DATA ENTRY CONTROLS-->
<DockPanel >
<!--HEADER WITH MARGIN TO PAD THE TOP-->
<TextBlock x:Name="signUpText"
Text="Sign up for Horizon Chat"
Foreground="White"
Margin="0,10,0,0"
DockPanel.Dock="Top"
FontSize="20"/>
<!--STACKPANEL FOR ORDERING CONTROLS-->
<StackPanel DockPanel.Dock="Bottom" Margin="5,0">
<!--TOP PADDING TEXTBLOCK-->
<TextBlock />
<!-- For the username box -->
<TextBlock x:Name="nameSignUpBoxDescription"
Text="Username" />
<TextBox x:Name="nameSignUpBox" />
<!-- For the email box -->
<TextBlock x:Name="emailSignUpBoxDescription"
Text="Email" />
<TextBox x:Name="emailSignUpBox" />
<!-- For the password box -->
<TextBlock x:Name="passwordSignUpBoxDescription"
Text="Password" />
<PasswordBox x:Name="passwordSignUpBox" />
<!--BOTTOM PADDING TEXTBLOCK-->
<TextBlock />
</StackPanel>
</DockPanel>
</Viewbox>
</Border>
</DockPanel>
</Window>