现在要以一个TextBlock做一个密码框的水印,现如今要判断密码框中是否存在密码,若有密码我让TextBlock显示,若是没有则不显示,但是我要怎么判断密码框是不是获取了焦点,以为TextBlock显示与否的逻辑是要在密码框没有取得失去焦点时写,怎么解决?

解决方案 »

  1.   

    给你参考,用WPF就别用那么土的方式去做事
                <TextBox Margin="5,5,5,5" Width="500" Height="25"
                        HorizontalAlignment="Left" HorizontalContentAlignment="Right">
                    <TextBox.Resources>
                        <VisualBrush x:Key="helpBrush" TileMode="Tile"
                                     Stretch="Fill"
                                     AlignmentX="Right" AlignmentY="Center">
                            <VisualBrush.Visual>
                                <Grid Background="{DynamicResource BasicTextBox_Background}"
                                      Width="500" Height="25">
                                    <TextBlock Foreground="Gray" Opacity="0.5" Margin="0,0,5,0"
                                               HorizontalAlignment="Right" VerticalAlignment="Center" >
                                        <Italic>我是水印哦  </Italic>
                                        <Run>(*^_^*)</Run>
                                    </TextBlock>
                                </Grid>
                            </VisualBrush.Visual>
                        </VisualBrush>
                        <Style TargetType="TextBox" >
                            <Style.Triggers>
                                <Trigger Property="Text" Value="{x:Null}">
                                    <Setter Property="Background" Value="{StaticResource helpBrush}"/>
                                </Trigger>
                                <Trigger Property="Text" Value="">
                                    <Setter Property="Background" Value="{StaticResource helpBrush}"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>                    
                    </TextBox.Resources>
                </TextBox>
      

  2.   

    你可以试试在密码框的onfocused事件中处理(onfocused是获取焦点事件)
      

  3.   


    这位大大PasswordBox里面是没有Text属性的。
      

  4.   

    我说了,给你参考,只是告诉你解决这个问题的思想,在WPF中用事件去控制UI的方式弱爆了。
      

  5.   


    嗯嗯,我现在是想用触发器去做这个了.不会Blend有点蛋疼.这位大大Blend的使用要怎么学比较好呢?
      

  6.   


    其实网上还是有很多怎么给Password添加水印的方法的.