2013-01-14 41 views
1

我正在寫一個基於WPF的應用程序,我遇到了一個問題。我已經搜索了幾個星期的答案,但仍找不到解決方案。問題是:我無法用小費顯示背景文字。我使用我自己的書面風格,並嘗試通過觸發器顯示文本。這裏是我的代碼示例:WPF。在文本框和密碼框中的背景幫助文本

<Style TargetType="{x:Type TextBox}" x:Key="DCTextBox">   
      <Setter Property="OverridesDefaultStyle" Value="True"/> 
      <Setter Property="FontSize" Value="14"/> 
      <Setter Property="Foreground" Value="#21346b"/> 
      <Setter Property="FontFamily" Value="Fonts/#BankGothic Md BT"/>    
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type TextBox}"> 
         <Border CornerRadius="5" BorderThickness="6" BorderBrush="#21346b" Background="White" > 
          <ScrollViewer Margin="0" x:Name="PART_ContentHost"/> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Style.Resources> 
        <VisualBrush x:Key="HelpBrush" Opacity="0.4" Stretch="None" AlignmentX="Left" > 
         <VisualBrush.Visual> 
          <TextBlock FontStyle="Italic" Text="Type or select from list" Background="Black"/> 
         </VisualBrush.Visual> 
        </VisualBrush> 
      </Style.Resources>   
      <Style.Triggers> 
       <Trigger Property="Text" Value="{x:Null}"> 
        <Setter Property="Control.Background" Value="{StaticResource HelpBrush}"/> 
       </Trigger> 
       <Trigger Property="Text" Value=""> 
        <Setter Property="Control.Background" Value="{StaticResource HelpBrush}"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 

請告訴我哪裏可能是問題?哦,還有一個問題:是否可以使用類似的方法在密碼箱中輸出背景文本?謝謝!

+0

你是在水印文本框之後?如果是這樣,之前已經介紹過了,下面是一個很好的例子:[WPF中的水印/提示文本TextBox](http://stackoverflow.com/q/833943/109702) – slugster

回答

0

如果我理解正確,你想要的是一個TextBox,它在空的時候顯示一些文本並且沒有關注,以便告訴用戶如何處理它。爲此,我建議創建一個從TextBox繼承的新控件,因此,當您設置樣式時,不會影響應用中的所有文本框。爲其添加一個DependencyProperty,以便您可以在XAML中設置幫助文本。

public class MyTextBox : TextBox 
{ 
    public static DependencyProperty LabelTextProperty = 
     DependencyProperty.Register(
      "LabelText", 
      typeof(string), 
      typeof(MyTextBox)); 
} 

定義你的風格,在這種情況下,我做到這一點(我只是張貼相關部分,你可以把它漂亮的自己看起來):

<Style x:Key="{x:Type local:MyTextBox}" TargetType="{x:Type local:MyTextBox}"> 
<Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:MyTextBox}"> 

    <ControlTemplate.Triggers> 
        <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
         <Setter Property="Visibility" TargetName="LabelText" Value="Hidden" /> 
        </Trigger> 
        <Trigger Property="HasText" Value="True"> 
         <Setter Property="Visibility" TargetName="LabelText" Value="Hidden" /> 
        </Trigger> 
    </ControlTemplate.Triggers> 
      </ControlTemplate> 
</Setter.Value> 
</Style> 

你使用這樣的:

<Local:MyTextBox LabelText="This is the tip for the user" Text="{Binding SomeProperty}"/> 

關於密碼箱,我沒有試過,但它應該工作得很好。如果LabelText顯示爲「xxxxxxxxx」,我相信你會找到一個解決方法(突然出現,我可以考慮在PasswordBox內創建一個類型爲TextBlock的DependencyProperty,使用Tip字符串設置其內容,並隱藏/顯示整個事情)。

最後一條建議:當您只想顯示文本時,不要花費使用VisualBrush的開銷,它會在資源方面過度使用,在某些情況下可能會導致渲染效果不佳。希望這有助於問候!

0

我做了一些更正,它適用於我。

<Style TargetType="{x:Type TextBox}" x:Key="DCTextBox"> 
     <Setter Property="FontSize" Value="14"/> 
     <Setter Property="Foreground" Value="#21346b"/> 
     <Setter Property="FontFamily" Value="Fonts/#BankGothic Md BT"/> 
     <Style.Resources> 
      <VisualBrush x:Key="HelpBrush" Opacity="0.4" Stretch="None" AlignmentX="Left" > 
       <VisualBrush.Visual> 
        <TextBlock FontStyle="Italic" Text="Type or select from list" Foreground="Black"/> 
       </VisualBrush.Visual> 
      </VisualBrush> 
     </Style.Resources> 
     <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> 

希望它有幫助。