How to Programmatically show a WPF/C# Windows.Control.ToolTip?

后端 未结 7 2024
温柔的废话
温柔的废话 2020-12-25 12:01

There doesn\'t seem to be a .Show() kind of method for the Windows.Control.ToolTip, incl in ToolTipService.

相关标签:
7条回答
  • 2020-12-25 12:38

    Is showing a tooltip what you really want to do. A tooltip has a clear meaning to most users and an expectation that it goes away when moving the mouse (and can come back when you hover over the item in question).

    If your aim is to draw attention to something, have you considered some form of floating box which is fully under your control, WPF makes this easy!

    0 讨论(0)
  • 2020-12-25 12:44

    If you would like to control how long the tooltip remains open, you can subscribe to the Opened event and set a time delay before closing the tooltip.

    Subscription has to be done before IsOpen = true and it has to be an async method to avoid hanging up the UI.

    var tooltip = new ToolTip { Content = "New tooltip text" };
    MyControln.ToolTip = tooltip;
    tooltip.Opened += async delegate (object o, RoutedEventArgs args)
    {
        var s = o as ToolTip;
        // let the tooltip display for 1 second
        await Task.Delay(1000);
        s.IsOpen = false;
        // wait till the close tooltip animation finishes before changing to old tooltip text
        await Task.Delay(1000);
        s.Content = "Old tooltip text";
    };
    tooltip.IsOpen = true;
    
    0 讨论(0)
  • 2020-12-25 12:53

    What you need to do is make sure the ToolTip on the control is of type ToolTip. Then you can set the IsOpen property to true like so:

    ToolTip tooltip = new ToolTip{ Content = "My Tooltip" };
    NameTextBox.ToolTip = tooltip;
    tooltip.IsOpen = true;    
    
    0 讨论(0)
  • 2020-12-25 12:53

    If you already design tooltip in XAML, you can try that way:

    ((ToolTip)Calendar01.ToolTip).IsOpen = true;
    
    0 讨论(0)
  • 2020-12-25 12:53

    Check out the IsOpen property in the ToolTipService class.

    0 讨论(0)
  • 2020-12-25 13:00

    Finally i ended with this .. and it's working fantastic ..

                Popup myPopup = new Popup();
                myPopup.PlacementTarget = control; //FrameworkElement where you want to show this tooltip             
                myPopup.Placement = PlacementMode.Top;
                myPopup.PopupAnimation = PopupAnimation.Slide;
    
                myPopup.AllowsTransparency = true;
                TextBlock popupText = new TextBlock();
                popupText.Text = ErrorMessage; //Message you want to show
                popupText.Background = Brushes.AliceBlue;
                popupText.Foreground = Brushes.Red;
                //popupText.FontSize = 12;                
                popupText.TextWrapping = TextWrapping.Wrap;
                myPopup.Child = popupText;
                // popup1.CustomPopupPlacementCallback =
                // new CustomPopupPlacementCallback(placePopup);
    
                //myPopup.HorizontalOffset = control.ActualWidth - popupText.ActualWidth;
                control.ToolTip = myPopup;
                myPopup.IsOpen = true;
                myPopup.StaysOpen = false;
    
    0 讨论(0)
提交回复
热议问题