C# 如何在WPF中的按钮悬停延迟一段时间后显示堆栈面板?
我有一个按钮,鼠标在这个按钮上悬停两秒钟,我想打开堆栈面板 xaml代码:C# 如何在WPF中的按钮悬停延迟一段时间后显示堆栈面板?,c#,wpf,C#,Wpf,我有一个按钮,鼠标在这个按钮上悬停两秒钟,我想打开堆栈面板 xaml代码: <StackPanel x:Name="spPinDetailsPopup" Height="200" Width="240" Visibility="Hidden"> <!--it contains label control which have text to display--> </StackPanel> <Button x:Name="btnDisplayP
<StackPanel x:Name="spPinDetailsPopup" Height="200" Width="240" Visibility="Hidden">
<!--it contains label control which have text to display-->
</StackPanel>
<Button x:Name="btnDisplayPopUp" MouseEnter="DisplayPopUpOnMouseEnter"/>
我使用了堆栈面板作为一个弹出窗口来显示控制信息
当前结果:堆栈面板立即显示
预期结果:
只有当我们将鼠标光标放在按钮上超过2秒时,堆栈面板才应打开。
有什么解决办法吗?为什么不使用工具提示
<Button ToolTip="Some Text" ToolTipService.InitialShowDelay="2000"/>
由于工具提示
是一个对象
,因此您可以根据自己的喜好配置布局,而不仅仅是一些文本
解决方案: 我在全局级别创建了一个Dispatchermer对象来控制打开弹出窗口的时间间隔
private static DispatcherTimer _popUpOpeningTimer = new DispatcherTimer();
void DisplayPopUpOnMouseEnter(object sender, MouseEventArgs e)
{
// if we do not have DispatcherTimer set then only initialize it
if (_popUpOpeningTimer == null)
{
_popUpOpeningTimer = new DispatcherTimer();
}
else // this means that dispatcher timer is already initialized, so do nothing
{
// do nothing
}
// stop the timer if it is already set so that we can start new timer
_popUpOpeningTimer.Stop();
_popUpOpeningTimer.Interval = new TimeSpan(0, 0, 1);
_popUpOpeningTimer.Tick += new EventHandler((senderObject, eventArguments) => DisplayPopUp(senderObject, eventArguments, btnDisplayPopUp));
// start the timer for opening the details popup
_popUpOpeningTimer.Start();
}
private void DisplayPopUp(object sender, EventArgs e, Button button)
{
if(button.IsMouseOver)
{
spPinDetailsPopup.Visibility = Visibility.Visible;
}
else
{
// do nothing
}
}
是的,试试看,我们会帮助你。正如你在上面写的,如果你没有很好地展示你的尝试,这个问题就太广泛了。如果/当你修正你的问题,使其足够好,值得回答时,你需要确保你也明确你所说的“打开堆栈面板”的意思。
StackPanel
对象不是“可关闭”的,所以不清楚“打开”一个对象意味着什么。感谢各位的回答,我已经更新了问题:)@Leblanc,用户要求在工具提示中显示简短信息,在弹出窗口中显示详细信息,所以我将stack panel用作弹出窗口。
private static DispatcherTimer _popUpOpeningTimer = new DispatcherTimer();
void DisplayPopUpOnMouseEnter(object sender, MouseEventArgs e)
{
// if we do not have DispatcherTimer set then only initialize it
if (_popUpOpeningTimer == null)
{
_popUpOpeningTimer = new DispatcherTimer();
}
else // this means that dispatcher timer is already initialized, so do nothing
{
// do nothing
}
// stop the timer if it is already set so that we can start new timer
_popUpOpeningTimer.Stop();
_popUpOpeningTimer.Interval = new TimeSpan(0, 0, 1);
_popUpOpeningTimer.Tick += new EventHandler((senderObject, eventArguments) => DisplayPopUp(senderObject, eventArguments, btnDisplayPopUp));
// start the timer for opening the details popup
_popUpOpeningTimer.Start();
}
private void DisplayPopUp(object sender, EventArgs e, Button button)
{
if(button.IsMouseOver)
{
spPinDetailsPopup.Visibility = Visibility.Visible;
}
else
{
// do nothing
}
}