如何在wpf中的文本框中添加水印文本?

时间:2013-07-09 16:41:01

标签: wpf textbox watermark passwordbox

我正在开发一个wpf应用程序。如何将水印文本添加到textboxpasswordbox

<Window x:Class="WpfApplication1.MainWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="Platforma Smart School 1.0" Height="580" Width="880" 
   Icon="/WpfApplication4;component/Images/capturennnnnn12_256px%20%282%291.ico">

<Grid>

    <Grid.Background>
       <ImageBrush ImageSource="/WpfApplication1;component/Images/
       hp-colorful-books-hd-105609.jpg" />
    </Grid.Background>

    <Rectangle Height="334" HorizontalAlignment="Left" Margin="608,162,0,0" 
    Name="rectangle1" Stroke="#FFDBD8D8" VerticalAlignment="Top" Width="222"
    Fill="#FFF0F0F0" />

    <Button Height="34" HorizontalAlignment="Left" Margin="630,434,0,0"
    Name="button1" VerticalAlignment="Top" Width="178" FontSize="13" 
    Foreground="#FF555555" />

    <TextBox Height="34" HorizontalAlignment="Left" Margin="630,219,0,0" 
    Name="textBox1" VerticalAlignment="Top" Width="178" FontSize="14" Text=""
    Foreground="#FF7C7A7A"  />

    <PasswordBox Height="32" HorizontalAlignment="Left" Margin="630,304,0,0"
    Name="passwordBox1" VerticalAlignment="Top" Width="178" FontSize="14"
    FontFamily="Segoe UI"/>

    <Image Height="134" HorizontalAlignment="Left" Margin="606,12,0,0" 
    Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="224"
    Source="/WpfApplication1;component/Images/Capture3.PNG" />

    <Label Content=" Smart School" Height="31" HorizontalAlignment="Left" 
    Margin="251,498,0,0" Name="label1" VerticalAlignment="Top" Width="227" />

</Grid>

3 个答案:

答案 0 :(得分:2)

看看这个SO Question。创建附属财产的第二个答案就是我的建议。

还有一些扩展的WPF控件可以提供帮助:http://wpftoolkit.codeplex.com/wikipage?title=WatermarkTextBox

编辑:添加了扩展WPF工具包链接。

答案 1 :(得分:0)

或者您可以尝试使用此代码

<Grid>
    <Grid.Resources>
        <VisualBrush x:Key="LoginHint" Stretch="None" AlignmentX="Left" AlignmentY="Top" >
                <VisualBrush.Transform>
                    <TranslateTransform X="5" Y="7" />
                </VisualBrush.Transform>
                <VisualBrush.Visual>
                    <Grid HorizontalAlignment="Left">
                      <TextBlock FontFamily="SEGOEWP"  FontSize="10"   FontWeight="Normal"
                           HorizontalAlignment="Left" VerticalAlignment="Center"                                     Foreground="Gray" FontStyle="Italic" Opacity="1"                                          Text="Enter Username"/>
                    </Grid>
                </VisualBrush.Visual>
            </VisualBrush>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label x:Name="lblUserName" Content="User Name" Grid.Column="0"                              VerticalAlignment="Top" Margin="5"/>
        <TextBox x:Name="waterMarkTextBox" Width="100" Height="25" Grid.Column="1" 
        VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5">
          <TextBox.Style>
             <Style TargetType="{x:Type TextBox}">
               <Setter Property="Background" Value="Transparent"/>
                 <Style.Triggers>
                   <DataTrigger Binding="{Binding ElementName=waterMarkTextBox,Path=Text}"                     Value="" >
                      <Setter Property="Background" Value="{StaticResource LoginHint}"/>
                   </DataTrigger>
                 </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
    </Grid>

答案 2 :(得分:0)

MahApps.Metro有一个内置的水印控件,使用起来相当简单。它适用于PasswordBox。

 <AdornerDecorator>
            <PasswordBox Name="password"
                     Width="200"
                     HorizontalAlignment="Right">
                <Controls:TextBoxHelper.Watermark>Password</Controls:TextBoxHelper.Watermark>
            </PasswordBox>
  </AdornerDecorator>