简体   繁体   English

Wpf - 显示文本框工具提示

[英]Wpf - Show Textbox ToolTip

<TextBox Name="txtInput">
<TextBox.ToolTip>
    <ToolTip Name="TestToolTip">
        This a test message
    </ToolTip>
</TextBox.ToolTip>

private void btnClick_Click_1(object sender, RoutedEventArgs e)
{
    txtInput.Focus();
    ToolTipTest.IsVisible = true;
}

When the button is clicked the tooltip is shown on the button, I want to simulate putting mouse pointer on the textbox, the toolbox to be shown for the textbox 单击按钮时,按钮上会显示工具提示,我想模拟将鼠标指针放在文本框上,为文本框显示工具箱

First of all, you should be using the standard validation for something like what you are doing. 首先,您应该使用标准验证来完成您正在做的事情。 From your comment above I can tell that's what you are doing and you should know that WPF has a really good builtin system for doing exactly what you want without doing it so imperatively (and very reusable). 从你上面的评论中我可以看出你正在做什么,你应该知道WPF有一个非常好的内置系统,可以完成你想做的事情而不必如此强制(并且非常可重复使用)。

Here's an example of a style you can apply to, say, all textboxes when the value the are bound to doesn't validate (using IDataErrorInfo). 下面是一个样式示例,例如,当绑定的值未验证时,可以应用于所有文本框(使用IDataErrorInfo)。

<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="ToolTip">
           <Setter.Value>
                <ToolTip Content="{Binding RelativeSource={RelativeSource Self}, 
                   Path=(Validation.Errors)[0].ErrorContent}" IsOpen="true" />
           </Setter.Value>
        </Setter>
    </Trigger>
</Style.Triggers>

You might also consider a more standard UI that utilizes the adorner layer to put a validation failure indicator next to the control that failed validation. 您还可以考虑使用更标准的UI,该UI利用adorner层将验证失败指示符放在验证失败的控件旁边。 Here's a sample on that: 这是一个示例:

http://blogsprajeesh.blogspot.com/2009/03/handling-error-in-wpf-idataerrorinfo.html http://blogsprajeesh.blogspot.com/2009/03/handling-error-in-wpf-idataerrorinfo.html

Good luck. 祝好运。

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

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