需要顯示提示,其中包含來自文本字段的數據。如果文本框有數據,提示顯示。如何添加和顯示工具提示文本框WPF如果文本框不爲空
1
A
回答
4
只需使用綁定到ToolTipService附加屬性。 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}}"/>
轉換器:
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();
}
}
3
您可以使用觸發器禁用的工具提示。將這種風格在你的窗口或應用程序的資源,以便它可以在這取決於你的選擇在你的窗口或應用程序的所有文本框共享 -
<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>
確保您的系統命名空間添加到您的XAML -
xmlns:sys="clr-namespace:System;assembly=mscorlib"
0
我試着用可見性模式& TextChange事件。當沒有文本時,ToolTip不可見。可能對別人有用。 的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事件處理程序:
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;
}
}
1
我有這個問題我自己想通了不同的解決方案。我知道這個問題已經回答了,但就像我仍然會有人碰到這個問題來了,我想和大家分享我的解決方案:背後
XAML
<TextBox Name="textBox1" ToolTip="{Binding Text, RelativeSource={RelativeSource Self}}" ToolTipService.IsEnabled="False"/>
代碼
private void textBox1_TextChanged(object sender, TextChangedEventArgs e) { if (textBox1.Text.Length > 0) { ToolTipService.SetIsEnabled(textBox1, true); } }
我希望這有助於某人。
相關問題
- 1. WPF - 顯示文本框工具提示
- 2. 顯示文本框工具提示
- 3. 如何向WPF文本框添加提示文本?
- 4. 工具提示文本框
- 5. 文本框邊框不顯示 - WPF
- 6. 如果文本框爲空不工作
- 7. 如果文本框爲空或使用C#顯示消息框
- 8. 文本框驗證工具提示中不顯示
- 9. 如何在JQuery中爲文本框製作工具提示?
- 10. 如何讓HTML文本框在空時顯示提示?
- 11. HTML文本框輸入工具提示?
- 12. C#中的工具提示文本框#
- 13. 文本框的氣球工具提示
- 14. 將工具提示添加到抖動文本框
- 15. Wpf Datagrid不顯示文本框數據?
- 16. c# - 如何設置文本框中的文本顯示提示時,文本框是空的?
- 17. 如果文本框爲空,如何填充文本框?
- 18. WPF:基於光標顯示並保留文本框的工具提示
- 19. 如果文本框爲空,則使用文本框爲單元格添加值
- 20. asp.net:treeview - 顯示文本框,如果選中?
- 21. 如何在使用dojo的文本框旁邊添加工具提示
- 22. ASP.NET GridView如果行爲空,則顯示文本框
- 23. 顯示一個div,如果文本框爲空(NG-重複)
- 24. 如何顯示無效輸入到文本框的工具提示
- 25. 關於文本框和按鈕的JQuery基本工具提示
- 26. 顯示工具提示,以MouseHover文本
- 27. Matlab編輯文本框 - 顯示提示?
- 28. 不保存,如果文本框爲空
- 29. C#文本框不顯示
- 30. 文本框不顯示