简体   繁体   English

WPF。 文本框和密码框中的后台帮助文本

[英]WPF. Background help text in textbox and passwordbox

I'm writing an WPF-based application and I've got a problem with It. 我正在编写一个基于WPF的应用程序,但我遇到了问题。 I've searched an answer for weeks, but still couldn't find the solution. 我已经搜索了数周的答案,但仍然找不到解决方案。 The problem is: I can't show the background text with a tip. 问题是:我无法显示带有提示的背景文本。 I'm using my own written style and trying to show text via triggers. 我正在使用自己的书面样式,并尝试通过触发器显示文本。 Here's the code sample I made: 这是我制作的代码示例:

<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>

Please, tell me where could be the problem? 请告诉我哪里可能出问题了? Oh, and one more question: is it possible to output the background text in passwordbox using the similar method? 哦,还有一个问题:是否可以使用类似的方法在密码箱中输出背景文本? Thank you! 谢谢!

If I understood correctly, what you want is a TextBox that displays a some text when empty and not focused, in order to tell the user what to do with it. 如果我理解正确,那么您想要的是一个文本框,该文本框在为空且未聚焦时显示一些文本,以便告诉用户如何处理它。 For this I suggest to create a new control inherited from TextBox, so when you set your style you don't affect ALL the TextBoxes in your app. 为此,我建议创建一个继承自TextBox的新控件,因此在设置样式时,不会影响应用程序中的所有TextBoxes。 Add a DependencyProperty to it so you can set the help text in XAML. 向其添加DependencyProperty,以便可以在XAML中设置帮助文本。

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

Define your style, in this case I do this (I'll just post the relevant parts, you can make it look pretty yourself): 定义您的样式,在这种情况下,我要这样做(我只张贴相关部分,您可以使自己看起来很漂亮):

 <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>

You use it like this: 您可以这样使用它:

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

Regarding the passwordbox, I havent't tried, but it should work just fine. 关于密码箱,我没有尝试过,但是应该可以正常工作。 If the LabelText is displayed as "xxxxxxxxx" I'm sure you'll find a workaround (out of the blue, I can think of creating a DependencyProperty of type TextBlock inside the PasswordBox, setting its content with the Tip string, and hiding/showing the whole thing). 如果LabelText显示为“ xxxxxxxxx”,我确定您会找到一种解决方法(突然之间,我可以考虑在PasswordBox中创建TextBlock类型的DependencyProperty,并使用Tip字符串设置其内容,然后隐藏/显示整个内容)。

One last suggestion: don't go into the expense of using a VisualBrush when you just want to display a text, it's overkill resourcewise and you might get a poor rendering in some cases. 最后一个建议:当您只想显示文本时,不要花费使用VisualBrush的开销,这在资源上是过分的,在某些情况下可能会得到较差的渲染。 Hope this helps, regards! 希望这会有所帮助,问候!

I did few corrections and it works fine for me. 我做了一些更正,对我来说很好。

 <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>

hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM