Search code examples
c#wpftooltipadorner

Adorner Tooltip


I am in the process of adding an adorner to our application which is going to give a visual representation of the connection strength.

public enum ConnectionState
{
    Good = 0,
    Poor = 1
}

The UI design requires a Tooltip ("Poor connection") to be displayed on mouse hover when the ConnectionState = Poor.

I am struggling with how to add a tooltip to my adorner, I have looked at using a button rather than the geometry I am using, but this seems like a hack.

Here is the code for my adorner as it stands at the moment.

public class ConnectionStatusAdorner : Adorner
{
    const ConnectionState DEFAULT_STATE = ConnectionState.Good;

    private readonly DrawingGroup _drawingGroup;
    private readonly RecordButton _recordButton;

    public ConnectionStatusAdorner(RecordButton recordButton)
        : base(recordButton)
    {
        _recordButton = recordButton ?? throw new ArgumentNullException(nameof(recordButton));

        _drawingGroup = new DrawingGroup();

        Focusable = false;
        IsHitTestVisible = false;

        SetConnectionState(DEFAULT_STATE);
    }

    public void SetConnectionState(ConnectionState state)
    {
        _drawingGroup.Children.Clear();

        var indicatorBrush = ResourceHelper.GetNetworkConnectionStatusBrush(state);
        var pen = new Pen(new SolidColorBrush(Colors.White), 1.5);

        var rect = new Rect
        {
            Location = new Point(_recordButton.Width - 8, 1.5),
            Size = new Size(10, 10)
        };

        var ellipse = new EllipseGeometry(rect);

        var drawing = new GeometryDrawing(indicatorBrush, pen, ellipse);

        _drawingGroup.Children.Add(drawing);
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        drawingContext.DrawDrawing(_drawingGroup);
    }
}

Any pointers would be very appreciated. Thanks in advance.


Solution

  • I am struggling with how to add a tooltip to my adorner ...

    I may miss something obvious here but can't you just set the ToolTip property of the adorner itself when you create it?:

    var adorner = new ConnectionStatusAdorner(button);
    adorner.ToolTip = "tooltip...";
    AdornerLayer.GetAdornerLayer(button).Add(adorner);
    

    Or directly in the class:

    public ConnectionStatusAdorner(RecordButton recordButton)
            : base(recordButton)
    {
        ...
        this.ToolTip = "...";
    }