views:

631

answers:

2

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

A: 

Not calling myself an expert, but I'd probably attempt such a feature with an attached property. This would be attachable to any element in your UI and you can specify an event handler that gets access to the object to which the property is being attached as well as the value passed to the attached property. You can keep a reference to the element to which your attached property was attached and you would then be able to change the ToolTip whenever you want.

flq
I like it, I'm registering a PropertyChangedCallback with the attached property so I can track all the elements as the property is set, just as you suggest - seems to work well so far, thanks
+1  A: 
  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>
Danny Varod