【问题标题】:C# WPF, dynamic tooltip, MouseLeftButtonUp not fireC# WPF,动态工具提示,MouseLeftButtonUp 不触发
【发布时间】:2018-09-10 16:59:05
【问题描述】:

有代码

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

【问题讨论】:

  • 不确定它是否有效,但您可以尝试将工具提示的 IsHitTestVisible 属性设置为 true。或者,您也许可以尝试使用 Popup 元素而不是 ToolTip(似乎您只想手动打开工具提示,而不是使用正常/传统的自动工具提示行为,这将使使用 Popup 可能是更好的选择)。

标签: c# wpf tooltip mouseevent


【解决方案1】:

谢谢。弹出它的工作正常

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;
}

【讨论】:

    【解决方案2】:

    ToolTips 窗口不能接受焦点,使用 Popup 控件代替你可以使用 Click On it

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

    【讨论】:

    • 我只是在这里吹毛求疵,但是(输入)焦点和鼠标动作/事件是两个不相关的概念。鼠标作为指向/定位设备(及其相关事件)不需要焦点,因为它“指向”或悬停在元素上会建立鼠标操作与该元素之间的关联。输入焦点用于诸如键盘输入之类的事情,其中​​按键本身不能与预期的接收控件建立关联。这种必要的关联是通过使用(输入)焦点机制来实现的。
    猜你喜欢
    • 2010-09-24
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多