Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you turn on word wrapping for all tool tips in Silverlight 4?

In my Silverlight 4 application I have some long tooltips. By default these tooltips appear one one very long line. For example:

        <TextBox Text="Test1" 
        ToolTipService.ToolTip="One tasdg asdg as da sdg asdg  asdg  asdg  asd gas dg a sdg a sdg a sd  a sd g asdasdgasdg    sadgasdgasdg  asdg  asdg  asd   as  a sd g a sdg      asd g asd g asd g asdgasdg     asdgasdg"/>

What I would like to do is to make the tooltips wrap around to appear on multiple lines. One way of achieving this is to define the tooltip using a TextBlock. For example:

        <TextBox Text="Test2"> 
        <ToolTipService.ToolTip>
            <TextBlock TextWrapping="Wrap" Width="200" Text="One tasdg asdg as da sdg asdg  asdg  asdg  asd gas dg a sdg a sdg a sd  a sd g asdasdgasdg    sadgasdgasdg  asdg  asdg  asd   as  a sd g a sdg      asd g asd g asd g asdgasdg     asdgasdg"/>             
        </ToolTipService.ToolTip>
    </TextBox>

Having to do this for every control that I want to define a tooltip seems like a lot of extra work. Ideally what I would like to do is to define the tooltips as strings like the first example, and then have a style globally applied to all ToolTips, which would make the tooltips wrap around. So in my App.xaml, I would define something like this:

        <Style TargetType="ToolTip">
        <!-- Somehow make all tooltips wrap at a width of 200 -->
    </Style>

Any advice on how I might go about doing this?

like image 241
Geoff Hardy Avatar asked Aug 05 '10 19:08

Geoff Hardy


1 Answers

You can create an implicit style for the tooltip and set the Content Template to something appropriate - e.g.

<Style TargetType="ToolTip">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock TextWrapping="Wrap" Width="200" Text="{Binding}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then you can use something like:

<TextBox Text="Test2" ToolTipService.ToolTip="abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg abcdefg " />
like image 104
Steve Willcock Avatar answered Sep 28 '22 08:09

Steve Willcock