窗口打开时文本消失

时间:2018-06-16 16:19:03

标签: c# wpf xaml resize window

所以,我有一个注册表单,在设计中,它看起来像这样:

What the sign up is supposed to look like

这是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);

现在,所有这些只与窗口有关,因为在我的情况下,我已经看到了所有的东西。因此,当我启动该程序时,我得到之外的所有内容(用户名,电子邮件和密码)上的小文本。就是这样:

What the window ends up as

现在,我的猜测是它会在调整大小期间将文本移动到某处,因为当我不调整窗口大小时,它仍然存在(就像在设计器中一样)。但是,如果将文本移动到其他位置仍然存在问题。我该如何解决这个问题?

编辑:我尝试调整设计器窗口的大小,这是调整大小100px之前和之后的步骤:

在:

Before

50%:

50%

之后:

After

1 个答案:

答案 0 :(得分:0)

使用@ 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>