C#WPF,动态工具提示,MouseLeftButtonUp不会激活

问题描述 投票:0回答:2

有代码

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事件永远不会激发。为什么?

c# wpf tooltip mouseevent
2个回答
0
投票

谢谢。随着弹出它的工作正常

System.Windows.Controls.Primitives.Popup popup;

...

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;

...

void popup_MouseLeave(object sender, MouseEventArgs e)
{
popup.IsOpen = false;
}

-1
投票

ToolTips窗口无法接受焦点,使用Popup控件可以使用Click On it

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

© www.soinside.com 2019 - 2024. All rights reserved.