How to Programmatically show a WPF/C# Windows.Cont

2019-02-02 22:19发布

问题:

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

回答1:

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;    


回答2:

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!



回答3:

ToolTip.Show() is available for Windows Forms, not for WPF controls. For WPF, if you simply want to display the ToolTip when the mouse enters the area of the control, you shouldn't need ToolTip.Show() if you write ToolTip="" in your XAML code (of the control for which you want the ToolTip) before the ToolTipOpening event in that control's XAML. For example, for a Button control:

<Button Name="exampleButton" Content="example" ToolTip="" ToolTipOpening="example_ToolTipOpening"/>

The ToolTip should then be displayed automatically every time the mouse enters the area of that control. (You can set which text to display in the ToolTipOpening event function. Or you can omit the ToolTipOpening and set the text in the quotation marks of the ToolTip="")

Hope this helps.



回答4:

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;


回答5:

Check out the IsOpen property in the ToolTipService class.



回答6:

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

((ToolTip)Calendar01.ToolTip).IsOpen = true;


回答7:

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;