locked
Trigger when Texttrimming is applied? RRS feed

  • Question

  • I have a TextBlock where I use TextTrimming=CharacterEllipsis. I want to show a tooltip over this TextBlock, but only if the text is cut off, is there a way to apply this?

    Tuesday, May 20, 2008 11:58 AM

Answers

  • I never did find the magic IsTextTrimmed dependency property I was looking for, so instead I created one.

    After spending way too much time digging around in the TextBlock class I determined that it violates the "lookless" tenets of WPF controls and makes very little effort to enable behavioral or appearance customizations. Rather than fight with it I decided to just subclass it and create my own custom control which I'll use any time I need enhanced customizability over TextBlock.

    My adventures are blogged here: http://tranxcoder.wordpress.com/2008/10/09/showing-tooltips-on-a-trimmed-textblock-wpf/

    May not be the solution you're looking for, but it does seem to meet my present need and I learned a bunch getting there.
    • Proposed as answer by hemp Thursday, October 9, 2008 11:50 PM
    • Marked as answer by _saga_ Monday, October 13, 2008 9:17 AM
    Thursday, October 9, 2008 11:50 PM

All replies

  • Its not really a nice solution but until something better comes arround...

    Code Snippet

    private void textblock_LayoutUpdated(object sender, EventArgs e)

    {

    textblock.TextWrapping = TextWrapping.Wrap;

    textblock.UpdateLayout();

    double height = textblock.ActualHeight;

    textblock.TextWrapping = TextWrapping.NoWrap;

    textblock.UpdateLayout();

    if (height > textblock.ActualHeight)

    {

    //text is trimmed

    }

    }

    Tuesday, May 20, 2008 4:33 PM
  • So there is no property for this?

    I need a more general solution since it's going to be used in a template.

    Wednesday, May 21, 2008 6:51 AM
  • Do you mean that the text shown in tooltip is trimmed? If so, the following example works well. You can check to see is there any differences from yours?

     

    Code Snippet

    <TextBlock x:Name="TextBlock" Width="100" TextTrimming="CharacterEllipsis" ToolTip="{Binding Text, ElementName=TextBlock}">

        In the following example, the ListBox displays the list of HorizontalAlignment enumeration values through data binding. The ListBox and the Button are bound such that you can change the HorizontalAlignment property value of the Button by selecting a value in the ListBox.

    </TextBlock>

     

     

    Best Regards,

    Wei Zhou

    Thursday, May 22, 2008 6:37 AM
  • No Wei, I mean that I only want to show a tooltip when hovering a textblock that has trimmed text. If the complete text is shown, I don't want any tooltip. The text in the textblock is a binding, therefor I don't know before how long the text will be.

    So an example

     

    My textblock output is

    1. "My short text" --> Here I don't want any tooltip at all hovering the textblock.

    2. "Here I have my very long text that's tri..." --> Here I want to have a tooltip saying "Here I have my very long text that's trimmed" when I hoover the textblock.

     

    I need a trigger or something that tells me if the text in the textblock got trimmed. If it did, I want to add a tooltip to that textblock, otherwise, the textblock shouldn't have any tooltip. Are you with me?

     

    Thursday, May 22, 2008 11:38 AM
  • You can set the ToolTip.Content to TextBlock too to get this functionality.

     

    Code Snippet

    <TextBlock x:Name="TextBlock" Width="100">

        <TextBlock.ToolTip>

            <ToolTip>

                <TextBlock TextTrimming="CharacterEllipsis" Width="200" Text="..."/>

            </ToolTip>

        </TextBlock.ToolTip>

    </TextBlock>

     

     

    Best Regards,

    Wei Zhou

    Friday, May 23, 2008 7:58 AM
  • Wei, I still don't think you are following me :/

    I try to explain again.

     

    When the text in my textblock IS NOT trimmed, there shouldn't be any tooltip (the content had enough space in the textblock not to be cut).

    When the text in my textblock IS trimmed, there should be a tooltip containing the text in the textblock (the content was too long for the textblock and the CharacterEllipsis kicked in).

     

     

    What I need is something like this -->

     

    Code Snippet

    <TextBlock x:Name="myTextBlock" Text="{Binding myText}" TextTrimming="CharacterEllipsis">
    <TextBlock.Triggers>
    <Trigger Property="TextBlock.GotTrimmedWithEllipsis" Value="True">
    <Setter TargetName="myTextBlock" Property="Tooltip" Value="{Binding myText}" />
    </Trigger>
    </TextBlock.Triggers>
    </TextBlock>

     

     

    The property in blue color is the property I'm looking for. I know there isn't any with that name, but I need one that means the same thing.

    There is no use to have a tooltip over a textblock that has all it's content visible. It's only needed where the text is cut with the "...". The control where the textblock lays isn't resizeable, therefor the user will never know the entire text if it's cut and have no tooltip. But since the shown text is a binding, there could be both text that gets cut and text that doesn't.

    Monday, May 26, 2008 6:46 AM
  • _saga_ said:

    When the text in my textblock IS NOT trimmed, there shouldn't be any tooltip (the content had enough space in the textblock not to be cut).


    Any resolution to this? I have the same need: ToolTip enabled only when the underlying text is clipped.

    Barring any better ideas, I'm planning to dig into the Tree via Mole and see if I can find some property we can bind to (directly or indirectly) that will provide a "IsTextTrimmed" boolean value. If I find something, I'll post it here.
    Wednesday, October 8, 2008 12:16 AM
  • Sounds good. I haven't any solution either. Haven't looked at it more after that, but I am still interested in a solution. Also if you find any workaround.
    Wednesday, October 8, 2008 8:48 AM
  • I never did find the magic IsTextTrimmed dependency property I was looking for, so instead I created one.

    After spending way too much time digging around in the TextBlock class I determined that it violates the "lookless" tenets of WPF controls and makes very little effort to enable behavioral or appearance customizations. Rather than fight with it I decided to just subclass it and create my own custom control which I'll use any time I need enhanced customizability over TextBlock.

    My adventures are blogged here: http://tranxcoder.wordpress.com/2008/10/09/showing-tooltips-on-a-trimmed-textblock-wpf/

    May not be the solution you're looking for, but it does seem to meet my present need and I learned a bunch getting there.
    • Proposed as answer by hemp Thursday, October 9, 2008 11:50 PM
    • Marked as answer by _saga_ Monday, October 13, 2008 9:17 AM
    Thursday, October 9, 2008 11:50 PM
  • If you are still looking for a good solution to this problem, follow the link in hemp's post above to the blog site, since making the above post, he has developed a new, extremely elegant solution that defines a class "TextBlockService" which utilizes the AttachedProperty mechanism to add the desired functionality to the existing WPF TextBlock class.  With his class you can add a Trigger to your TextBlock Style to automatically turn on the ToolTip:

            <Style.Triggers> 
                <!-- Display a ToolTip containing the full text if the TextBlock is currently trimmed (clipped) --> 
                <MultiTrigger> 
                    <MultiTrigger.Conditions> 
                        <Condition Property="tbs:TextBlockService.AutomaticToolTipEnabled" Value="True" /> 
                        <Condition Property="tbs:TextBlockService.IsTextTrimmed" Value="True" /> 
                        <Condition Property="TextWrapping" Value="NoWrap" /> 
                    </MultiTrigger.Conditions> 
     
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Text}" /> 
                </MultiTrigger> 
            </Style.Triggers> 
     

    He has a complete Solution that includes the Source code and a sample application showing how to use it, VERY nice! Thanks hemp!!!
    Saturday, December 6, 2008 12:44 AM
  • so, any chance I can get my hopes up that .NET 4 will add an "IsTextTrimmed" dp to the TextBlock class?

    hemp's TextBlockService class is quite nice, but doesn't work for TextBlocks that use any form of Wrapping. It seems like it would be SO easy for MS to add this capability to the class and it would be extremely useful.

    UPDATE: I just discovered that the TextBlockService class has been modified so that it does now handle wrapped TextBlocks (nice!).  When you get the changes be sure to include the special case code described in the comments to handle single word text blocks.
    Friday, August 14, 2009 7:48 PM
  • Hi Saga

    Why donit you try this?

    double width=Utils.GetWidth(TextBlock.Text, TextBlock.Foreground, TextBlock.FontSize);

    Define a Util method to find the width of the text based on the Text Formats. Then compare the textblock actualsize with this width, if it is lesser than this width, place a popup or tooltip over the textblock.

    Regards,
    Jawahar
    Monday, January 25, 2010 12:36 PM
  • In essence, Jawahar, that's what my TextBlockService class does. However, getting access to that data via simple binding requires some WPF magic (aka: Attached Properties) which is why it turned into a long blog post and sample code.

    There are a lot of comments on that post from some very helpful fellow WPFers which enable the service to handle a wide variety of scenarios. Apart from subclassing or waiting for MS to release an enhanced version of the TextBlock control, it seems like the aforementioned TextBlockService is probably the most robust solution.

    - Shawn
    Monday, January 25, 2010 6:01 PM
  • Since this is potentially relevant to someone, Silverlight 4 (currently Beta) introduced a new TextTrimming property to the TextBlock class.

    TextTrimming

    The TextBlock control has a new property called TextTrimming that enables you to use the WordElipse trimming value.  When the property is set, any text exceeding the visible limit of the control will be truncated and an ellipsis will be displayed to the user indicating more content.


    Silverlight and WPF are very similar, but at the same time very different. If anyone has successfully adapted the TextBlockService to work for Silverlight (or found an alternative solution that enables automatic tooltips) please post up here. If not, I'm hoping to tackle that problem soon and will definitely make an update available (assuming I can achieve some degree of success.) At this point I haven't even tried it - just occured to me while reading about SL4 changes that I should post something here.
    Monday, February 1, 2010 6:40 PM