2016-11-22 90 views
0

我正在爲wpf應用程序及其文本框創建登錄窗口,我想要執行的操作與「搜索Q & A」 Stack Overflow網站的右上角表現正常。如何創建文本框提示,類似於SO的搜索框

有包含文本「用戶名」文本框中,當你專注,它仍然顯示的文本,但是當你開始打字,文字消失,您的輸入將被顯示出來。那麼當您嘗試將其抹除並且文本框中沒有內容時,它將恢復爲「用戶名」文本。

回答

0

你正在尋找該術語是文本框提示

試試這個:

<Window.Resources> 
    <VisualBrush x:Key="SearchHint" TileMode="None" Stretch="None" AlignmentX="Left"> 
     <VisualBrush.Transform> 
      <TranslateTransform X="5" Y="0" /> 
     </VisualBrush.Transform> 
     <VisualBrush.Visual> 
      <Grid> 
       <TextBlock FontStyle="Italic" Foreground="Black" Opacity="0.3" Text="Search …"/> 
      </Grid> 
     </VisualBrush.Visual> 
    </VisualBrush> 
</Window.Resources> 

<StackPanel> 
    <TextBox VerticalContentAlignment="Center" Height="30"> 
     <TextBox.Style> 
      <Style TargetType="TextBox"> 
       <Style.Triggers> 
        <Trigger Property="Text" Value=""> 
         <Setter Property="Background" Value="{StaticResource SearchHint}"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </TextBox.Style> 
    </TextBox> 
</StackPanel> 

結果:

enter image description here

+0

非常感謝你的生命的救星<3 –

+0

@JulioMotol歡迎您,雖然它您接受和/或給予好評答案的意思,如果它是正確的和很有幫助:-) – Jim

+0

但事情是,它被放置在網格上有一個藍色的背景和現在,文本框還具有一個藍色BG自上字母。 –

相關問題