首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# WPF,动态工具提示,MouseLeftButtonUp不触发

C# WPF,动态工具提示,MouseLeftButtonUp不触发
EN

Stack Overflow用户
提问于 2018-09-09 23:57:26
回答 2查看 95关注 0票数 0

有代码

代码语言:javascript
复制
ToolTip tt;

private void Grid_Loaded(object sender, RoutedEventArgs e)
{

    Label l = new Label();
    l.Content = "ToolTip";
    l.MouseLeftButtonUp += l_MouseLeftButtonUp;
    Grid.SetColumn(l, 0);
    Grid.SetRow(l, 0);
    grid.Children.Add(l);

    tt = new ToolTip();
    tt.StaysOpen = true;
    tt.MouseLeftButtonUp += tt_MouseLeftButtonUp;
    tt.Content = "12345";

}

void l_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{

    tt.IsOpen = true;

}

void tt_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{

    throw new NotImplementedException();

}

当鼠标单击标签时,会显示工具提示。如果工具提示鼠标单击了tt_MouseLeftButtonUp事件,则永远不会触发。为什么?

EN

回答 2

Stack Overflow用户

发布于 2018-09-10 16:59:06

谢谢。使用popup可以很好地工作

代码语言:javascript
复制
System.Windows.Controls.Primitives.Popup popup;

..。

代码语言:javascript
复制
StackPanel sp = new StackPanel();
sp.Margin = new Thickness(10, 10, 10, 10);

Label l1 = new Label();
l1.Content = "Test label 1";
l1.Tag = 1;
l1.MouseLeftButtonUp += l1_MouseLeftButtonUp;

sp.Children.Add(l1);

Border b = new Border();
b.BorderBrush = System.Windows.Media.Brushes.Black;
b.Background = System.Windows.Media.Brushes.White;
b.BorderThickness = new Thickness(1);
b.CornerRadius = new CornerRadius(10);
b.Child = sp;

popup = new System.Windows.Controls.Primitives.Popup();
popup.Child = b;
popup.AllowsTransparency = true;
popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Mouse;
popup.MouseLeave += popup_MouseLeave;

..。

代码语言:javascript
复制
void popup_MouseLeave(object sender, MouseEventArgs e)
{
popup.IsOpen = false;
}
票数 0
EN

Stack Overflow用户

发布于 2018-09-10 00:27:23

ToolTips窗口不能接受焦点,请使用Popup控件,您可以使用单击它

您可以创建保持打开状态的工具提示,直到用户单击其他位置。但是,ToolTipService.ShowDuration属性提供了工具提示自动消失(默认为5秒)或用户将鼠标移开之前的时间。如果您想要创建一个类似工具提示的窗口,该窗口将无限期地打开,最简单的方法是使用Popup控件。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52246238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档