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

时间:2018-09-09 15:57:26

标签: c# wpf tooltip mouseevent

有代码

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

2 个答案:

答案 0 :(得分:0)

谢谢。通过弹出窗口可以正常工作

remove()

...

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;

答案 1 :(得分:-1)

“工具提示”窗口无法接受焦点,请使用弹出控件,而不是单击“单击”。

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