views:

113

answers:

1

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?

+1  A: 

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 " />
Steve Willcock
Perfect! Thanks Steve, that was just what I was looking for. One minor change I made to this solution was to use MaxWidth rather than Width, which helps make the tooltip the right size.
Geoff Hardy