2

我正在学习 XAML Metro Style Application 的阶段。我想使用 WatermarkTextBox 控件创建简单的登录页面,如下图所示。我的问题是,我想将 WatermarkTextBox 的水印文本(电子邮件 ID 作为密码文本)垂直居中。

在此处输入图像描述

我的 XMAL 代码行如下

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Login"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Controls="using:WinRTXamlToolkit.Controls"
    x:Class="Login.MainPage"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="163*"/>
            <RowDefinition Height="101*"/>
            <RowDefinition Height="112*"/>
            <RowDefinition Height="114*"/>
            <RowDefinition Height="116*"/>
            <RowDefinition Height="162*"/>
        </Grid.RowDefinitions>
        <Controls:WatermarkTextBox WatermarkText="Email Id"   HorizontalAlignment="Left" Margin="502,10,0,0" Grid.Row="1" VerticalAlignment="Center" Height="60" Width="433" FontFamily="Andalus" FontSize="18" Padding="10, 15, 0, 0"/>
        <TextBox x:Name="PasswordWatermark" Text="Password"  IsHitTestVisible="False" Grid.Row="2" FontSize="18" FontFamily="Andalus" Width="433" Height="60" Margin="502,0,425,52" Padding="10, 15, 0, 0"/>
        <PasswordBox x:Name="pbPassword" LostFocus="PasswordLostFocus" GotFocus="PasswordGotFocus" Grid.Row="2" FontFamily="Andalus" FontSize="18" Margin="502,0,425,52" Opacity="0" Width="433" Height="60" Padding="10, 15, 0, 0"/>
        <Button x:Name="btnLogin" Content="Login" HorizontalAlignment="Left" Margin="791,0,0,0" Grid.Row="3" VerticalAlignment="Top" Height="58" Width="147" FontFamily="Andalus" FontSize="18" Background="#FF385936" BorderBrush="#FF644F4F"/>
    </Grid>
</Page>

。CS

private void PasswordLostFocus(object sender, RoutedEventArgs e)
{
    CheckPasswordWatermark();
}

private void CheckPasswordWatermark()
{
     var passwordEmpty = string.IsNullOrEmpty(pbPassword.Password);
     PasswordWatermark.Opacity = passwordEmpty ? 100 : 0;
     pbPassword.Opacity = passwordEmpty ? 0 : 100;
}

private void PasswordGotFocus(object sender, RoutedEventArgs e)
{
     PasswordWatermark.Opacity = 0;
     pbPassword.Opacity = 100;
}

非常感谢您的帮助。提前致谢。

4

2 回答 2

0

好的,我没有使用 Vs 2012,所以我无法测试它。我在记事本中编码。但这可能会有所帮助:

 <Controls:WatermarkTextBox WatermarkText="Email Id"   HorizontalAlignment="Left" Margin="502,10,0,0" Grid.Row="1" 
     VerticalAlignment="Center" Height="60" Width="433" FontFamily="Andalus" FontSize="18" Padding="10, 15, 0, 0">
    <Controls:WatermarkTextBox.WatermarkTextStyle>
        <Style TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center" />  
        </Style>            
    </Controls:WatermarkTextBox.WatermarkTextStyle>         
 <Controls:WatermarkTextBox/>

如果可能的话,告诉我结果。

于 2013-05-21T14:51:36.210 回答
0

我可能错了,但文本框包含 VerticalContentAlignment 和 Horizo​​ntalContentAlignment 属性。

这些是告诉控件如何对齐其中的文本的那些。

于 2013-07-18T13:47:45.110 回答