Earlz
Earlz

Reputation: 63825

Best way to bind tooltip text to content of a control?

So, I'm adding tooltips to a WPF form, and basically the tooltips only need to match the content. However, I have _ in some controls so that they can be selected with ALT+<key>. I of course do not want the _ to be included in the tooltip text.

Also, I'd prefer not having to write the name of the control out a third time for ElementName=. This is what I currently have:

<CheckBox x:Uid="chkProcess" Name="chkProcess" ToolTip="{Binding ElementName=chkProcess, Path=Content}">_Process widgets</CheckBox>

I also have a second method that works but isn't so pretty. It basically attaches to the TollTipOpening event and dynamically changes the tooltip value to match the content with proper stripping of _ characters.

Is there a clean way of stripping out the _ with databinding or a better way to do this in general?

Upvotes: 1

Views: 1997

Answers (2)

Shivani Katukota
Shivani Katukota

Reputation: 859

You could try this : Use label in tooltip that will not show "_"

<CheckBox x:Uid="chkProcess" Name="chkProcess">_Process widgets
    <CheckBox.ToolTip>
        <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
            <Label Content="{Binding Content}"/>
        </ToolTip>
    </CheckBox.ToolTip>
</CheckBox>

Upvotes: 2

Mike Strobel
Mike Strobel

Reputation: 25623

You can replace ElementName=chkProcess with RelativeSource={RelativeSource Self}. While it's more verbose, it might allow you to remove the Name attribute as well.

If you want to strip out the _ characters, you can create a simple IValueConverter to perform that task, and specify the Converter on your tool tip Binding. However, having a tool tip that simply regurgitates the label that's already displayed seems pointless at best, and annoying at worst.

Upvotes: 1

Related Questions