Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Dynamic tooltip

I have a class ToolTipProvider which has a method

string GetToolTip(UIElement element)

which will return a specific tooltip for the UIElement specified, based on various factors including properties of the UIElement itself and also looking up into documentation which can be changed dynamically. It will also probably run in a thread so when the form first fires up the tooltips will be something like the visual studio 'Document cache is still being constructed', then populated in the background.

I want to allow this to be used in any wpf form with the minimum effort for the developer. Essentially I want to insert an ObjectDataProvider resource into the Window.Resources to wrap my ToolTipProvider object, then I think I need to create a tooltip (called e.g. MyToolTipProvider) in the resources which references that ObjectDataProvider, then on any element which requires this tooltip functionality it would just be a case of ToolTip="{StaticResource MyToolTipProvider}" however I can't work out a) how to bind the actual elemnt itself to the MethodParameters of the objectdataprovider, or b) how to force it to call the method each time the tooltip is opened.

Any ideas/pointers on the general pattern I need? Not looking for complete solution, just any ideas from those more experienced


1 Answers

  1. Create a new user control which functions as a tool-tip view factory.

  2. Use your control as the tool-tip, passing any data you need for the factory to your control using binding (e.g. the data, the containing control, ...)

<AnyControl>
    <AnyControl.ToolTip>
        <YourToolTipControl Content="{Binding}" />
    </AnyControl.ToolTip>
</AnyControl>
like image 195
Danny Varod Avatar answered Jul 29 '26 16:07

Danny Varod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!