在 3d 对象的鼠标悬停时显示工具提示
Displaying tooltip on mouse hover of a 3d object
我正在使用 Helix 来显示一个简单的对象,如下所示
<h:HelixViewport3D >
<h:DefaultLights/>
<h:Teapot/>
</h:HelixViewport3D>
如何在鼠标悬停在茶壶上时显示工具提示?
谢谢
我会用这样的东西:
class ToolTipHelper
{
private readonly ToolTip _toolTip;
private readonly Timer _timer;
/// <summary>
/// Creates an instance
/// </summary>
public ToolTipHelper()
{
_toolTip = new ToolTip();
_timer = new Timer { AutoReset = false};
_timer.Elapsed += ShowToolTip;
}
/// <summary>
/// Gets or sets the text for the tooltip.
/// </summary>
public object ToolTipContent { get{ return _toolTip.Content; } set{ _toolTip.Content = value; } }
/// <summary>
/// To be called when the mouse enters the ui area.
/// </summary>
public void OnMouseEnter(object sender, MouseEventArgs e)
{
_timer.Interval = ToolTipService.GetInitialShowDelay(Application.Current.MainWindow);
_timer.Start();
}
private void ShowToolTip(object sender, ElapsedEventArgs e)
{
_timer.Stop();
if (_toolTip != null)
_toolTip.Dispatcher.Invoke(new Action(() => { _toolTip.IsOpen = true; }));
}
/// <summary>
/// To be called when the mouse leaves the ui area.
/// </summary>
public void OnMouseLeave(object sender, MouseEventArgs e)
{
_timer.Stop();
if (_toolTip != null)
_toolTip.IsOpen = false;
}
然后像这样修改Teapot
:
class Teapot
{
private readonly _tooltipHelper = new ToolTipHelper{ ToolTipContent = "MyToolTip" }; // keep the ToolTipHelper during the life of your Teapot but replace the Content whenever you want
private ModelUIElement3D _uiModel; // this has to be created and have its Model set to the suitable GeometryModel3D. You may want to replace an existing ModelVisual3D by this.
public Teapot(/*...*/)
{
_uiModel.MouseEnter += tooltipHelper.OnMouseEnter;
_uiModel.MouseLeave += tooltipHelper.OnMouseLeave;
//...
}
// ...
}
Xaml如果想修改这个定义tooltip的内容应该不难。
我正在使用 Helix 来显示一个简单的对象,如下所示
<h:HelixViewport3D >
<h:DefaultLights/>
<h:Teapot/>
</h:HelixViewport3D>
如何在鼠标悬停在茶壶上时显示工具提示?
谢谢
我会用这样的东西:
class ToolTipHelper
{
private readonly ToolTip _toolTip;
private readonly Timer _timer;
/// <summary>
/// Creates an instance
/// </summary>
public ToolTipHelper()
{
_toolTip = new ToolTip();
_timer = new Timer { AutoReset = false};
_timer.Elapsed += ShowToolTip;
}
/// <summary>
/// Gets or sets the text for the tooltip.
/// </summary>
public object ToolTipContent { get{ return _toolTip.Content; } set{ _toolTip.Content = value; } }
/// <summary>
/// To be called when the mouse enters the ui area.
/// </summary>
public void OnMouseEnter(object sender, MouseEventArgs e)
{
_timer.Interval = ToolTipService.GetInitialShowDelay(Application.Current.MainWindow);
_timer.Start();
}
private void ShowToolTip(object sender, ElapsedEventArgs e)
{
_timer.Stop();
if (_toolTip != null)
_toolTip.Dispatcher.Invoke(new Action(() => { _toolTip.IsOpen = true; }));
}
/// <summary>
/// To be called when the mouse leaves the ui area.
/// </summary>
public void OnMouseLeave(object sender, MouseEventArgs e)
{
_timer.Stop();
if (_toolTip != null)
_toolTip.IsOpen = false;
}
然后像这样修改Teapot
:
class Teapot
{
private readonly _tooltipHelper = new ToolTipHelper{ ToolTipContent = "MyToolTip" }; // keep the ToolTipHelper during the life of your Teapot but replace the Content whenever you want
private ModelUIElement3D _uiModel; // this has to be created and have its Model set to the suitable GeometryModel3D. You may want to replace an existing ModelVisual3D by this.
public Teapot(/*...*/)
{
_uiModel.MouseEnter += tooltipHelper.OnMouseEnter;
_uiModel.MouseLeave += tooltipHelper.OnMouseLeave;
//...
}
// ...
}
Xaml如果想修改这个定义tooltip的内容应该不难。