.net 如何以编程方式显示 WPF/C# Windows.Control.ToolTip?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1502964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Programmatically show a WPF/C# Windows.Control.ToolTip?
提问by MrGreggles
There doesn't seem to be a .Show() kind of method for the Windows.Control.ToolTip, incl in ToolTipService.
Windows.Control.ToolTip 似乎没有 .Show() 类型的方法,包括 ToolTipService。
回答by Ray
What you need to do is make sure the ToolTip on the control is of type ToolTip. Then you can set the IsOpen property to true like so:
您需要做的是确保控件上的 ToolTip 是 ToolTip 类型。然后,您可以将 IsOpen 属性设置为 true,如下所示:
ToolTip tooltip = new ToolTip{ Content = "My Tooltip" };
NameTextBox.ToolTip = tooltip;
tooltip.IsOpen = true;
回答by Anthony
If you would like to control how long the tooltip remains open, you can subscribe to the Openedevent and set a time delay before closing the tooltip.
如果您想控制工具提示保持打开状态的时间,您可以订阅该Opened事件并在关闭工具提示之前设置一个时间延迟。
Subscription has to be done before IsOpen = trueand it has to be an async method to avoid hanging up the UI.
订阅必须在之前完成IsOpen = true并且它必须是一个异步方法以避免挂断 UI。
var tooltip = new ToolTip { Content = "New tooltip text" };
MyControln.ToolTip = tooltip;
tooltip.Opened += async delegate (object o, RoutedEventArgs args)
{
var s = o as ToolTip;
// let the tooltip display for 1 second
await Task.Delay(1000);
s.IsOpen = false;
// wait till the close tooltip animation finishes before changing to old tooltip text
await Task.Delay(1000);
s.Content = "Old tooltip text";
};
tooltip.IsOpen = true;
回答by Moumit
Finally i ended with this .. and it's working fantastic ..
最后我以这个结束..它工作得很好..
Popup myPopup = new Popup();
myPopup.PlacementTarget = control; //FrameworkElement where you want to show this tooltip
myPopup.Placement = PlacementMode.Top;
myPopup.PopupAnimation = PopupAnimation.Slide;
myPopup.AllowsTransparency = true;
TextBlock popupText = new TextBlock();
popupText.Text = ErrorMessage; //Message you want to show
popupText.Background = Brushes.AliceBlue;
popupText.Foreground = Brushes.Red;
//popupText.FontSize = 12;
popupText.TextWrapping = TextWrapping.Wrap;
myPopup.Child = popupText;
// popup1.CustomPopupPlacementCallback =
// new CustomPopupPlacementCallback(placePopup);
//myPopup.HorizontalOffset = control.ActualWidth - popupText.ActualWidth;
control.ToolTip = myPopup;
myPopup.IsOpen = true;
myPopup.StaysOpen = false;
回答by Ray Hayes
Is showing a tooltip what you really want to do. A tooltip has a clear meaning to most users and an expectation that it goes away when moving the mouse (and can come back when you hover over the item in question).
正在显示您真正想要做什么的工具提示。工具提示对大多数用户具有明确的含义,并且期望在移动鼠标时它会消失(并且可以在您将鼠标悬停在相关项目上时返回)。
If your aim is to draw attention to something, have you considered some form of floating box which is fully under your control, WPF makes this easy!
如果您的目标是引起对某事的注意,您是否考虑过某种形式的完全在您控制之下的浮动框,WPF 让这一切变得简单!
回答by Malka
ToolTip.Show()is available for Windows Forms, not for WPF controls.
For WPF, if you simply want to display the ToolTip when the mouse enters the area of the control, you shouldn't need ToolTip.Show()if you write ToolTip=""in your XAML code (of the control for which you want the ToolTip) before the ToolTipOpeningevent in that control's XAML.
For example, for a Button control:
ToolTip.Show()可用于 Windows 窗体,不适用于 WPF 控件。对于 WPF,如果您只是想在鼠标进入控件区域时显示工具提示,那么ToolTip.Show()如果您在该控件的事件ToolTip=""之前写入XAML 代码(您想要工具提示的控件的代码),则不需要ToolTipOpeningXAML。例如,对于 Button 控件:
<Button Name="exampleButton" Content="example" ToolTip="" ToolTipOpening="example_ToolTipOpening"/>
The ToolTip should then be displayed automatically every time the mouse enters the area of that control. (You can set which text to display in the ToolTipOpening event function. Or you can omit the ToolTipOpeningand set the text in the quotation marks of the ToolTip="")
每次鼠标进入该控件的区域时,工具提示应自动显示。(可以在ToolTipOpening事件函数中设置要显示的文本。也可以省略ToolTipOpening,设置在引号中的文本ToolTip="")
Hope this helps.
希望这可以帮助。
回答by Konamiman
Check out the IsOpenproperty in the ToolTipServiceclass.
查看类中的IsOpen属性ToolTipService。
回答by zquanghoangz
If you already design tooltip in XAML, you can try that way:
如果您已经在 XAML 中设计了工具提示,则可以尝试这种方式:
((ToolTip)Calendar01.ToolTip).IsOpen = true;

