简体   繁体   English

如果文本框不为空,如何添加和显示工具提示文本框WPF

[英]How add and show tooltip textbox WPF if textbox not empty

Need to show a hint, which contains data from a text field. 需要显示一个提示,其中包含来自文本字段的数据。 Prompt to appear if the textbox has data. 如果文本框包含数据,则提示显示。

Just use binding to ToolTipService attached properties. 只需使用对ToolTipService附加属性的绑定即可。 XAML: XAML:

<UserControl.Resources>
    <converters:IsStringNonemptyConverter x:Key="ToolTipVisibilityConveter" />
</UserControl.Resources>

<TextBox Name="textBox" VerticalAlignment="Center" HorizontalAlignment="Center" Width="150"
         ToolTipService.ToolTip="{Binding Text, RelativeSource={RelativeSource Self}}"
         ToolTipService.IsEnabled="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource ToolTipVisibilityConveter}}"/>

Converter: 转换器:

internal sealed class IsStringNonemptyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return !String.IsNullOrEmpty(value as string);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

You can disable the tooltip using triggers. 您可以使用触发器禁用工具提示。 Place this style in your window or App resources so that it can be shared across all the textboxes in your window or application depending on your choice - 将此样式放在您的窗口或应用程序资源中,以便可以根据您的选择在窗口或应用程序中的所有文本框中共享该样式-

<Style x:Key="{x:Type TextBox}" TargetType="TextBox">
  <Style.Triggers>
     <Trigger Property="ToolTip" Value="{x:Static sys:String.Empty}">
        <Setter Property="ToolTipService.IsEnabled" Value="False" />
      </Trigger>
</Style.Triggers>

Make sure you add the system namespace to your xaml - 确保将系统名称空间添加到xaml-

xmlns:sys="clr-namespace:System;assembly=mscorlib"

I had this problem myself and figured out a different solution. 我自己遇到了这个问题,并找到了不同的解决方案。 I know this question has been answered but just like me there will still be people coming across this question, and I would like to share my solution: 我知道这个问题已经回答了,但就像我一样,仍然会有人遇到这个问题,我想分享一下我的解决方案:

XAML XAML

<TextBox Name="textBox1" ToolTip="{Binding Text, RelativeSource={RelativeSource Self}}" ToolTipService.IsEnabled="False"/>

Code behind 后面的代码

private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
    if (textBox1.Text.Length > 0)
    {
        ToolTipService.SetIsEnabled(textBox1, true);
    }
}

I hope this helps someone. 我希望这可以帮助别人。

I tried with Visibility Mode & TextChange event. 我尝试了“可见性模式”和“ TextChange”事件。 ToolTip invisible when no text. 没有文字时,工具提示不可见。 May be useful for someother. 可能对其他人有用。 Xaml: Xaml:

    <TextBox Height="23" Width="100" Name="myTextBox" TextChanged="myTextBox_TextChanged" >
        <TextBox.ToolTip>
            <ToolTip Visibility="Hidden">
                <TextBlock Name="toolTipTextBlock"></TextBlock>
            </ToolTip>            
        </TextBox.ToolTip>
    </TextBox>

TextChange event handler: TextChange事件处理程序:

    private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox tb = sender as TextBox;

        if (tb.Text.Trim() == "")
        {
            ((ToolTip)tb.ToolTip).Visibility = Visibility.Hidden;
        }
        else
        {
            toolTipTextBlock.Text = tb.Text;
            ((ToolTip)tb.ToolTip).Visibility = Visibility.Visible;
        }
    }

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

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