In Xaml TextBox with a Watermark that Disappears after First Iinput

前端 未结 4 1135
野趣味
野趣味 2021-01-06 08:57

In a TextBox provide a description that vanishes after the first input. This should provide a little help to the user what he should enter in the text field such as Watermar

4条回答
  •  时光说笑
    2021-01-06 09:38

    I have put together a complete example based on the provided link from anvarbek raupov (http://blogs.windowsclient.net/swt62/archive/2009/05/10/wpf-textbox-watermark-the-easy-way.aspx). The trick is to add an additional label above the text box, that is only displayed when then content of the text box is empty and not focused. The condition is implemented in XAML using Triggers. My example style lets the text box style unchanged from the normal behavior.

    The example consists of a commented resource dictionary (WatermarkResource.xaml) and a MainWindow.xaml with a normal and a watermarked text box. The code behind does only the initialization and is unchanged from a wizard generated WPF application.

    This is the WatermarkResource.xaml:

    
        
        
    
    

    And this is the MainWindow.xaml:

    
        
            
                
                    
                
            
        
        
            
                
                
            
            
                
                
            
            
    
    

    This is a screenshot of the running application with the visible Watermark

    Watermark, the XAML way

    and this is with some text, where the Watermark is hidden

    hidden Watermark

提交回复
热议问题