locked
Wrapping Label? RRS feed

  • Question

  • Anyone know how to make a Label that wraps across multiple lines in WPF?

    I can't seem to find a property that makes this work.

    Saturday, February 11, 2006 3:44 PM

Answers

  • Ah! Here's the story with mnemonics:
    AccessText is the control that does the work to parse the "_" and register the mnemonic.
    You might ask: where’s the AccessText in Button, Label, Menu, etc?

    The answer: it’s generated.

    The templates for all of these controls have a ContentPresenter. An additional property is set on the ContentPresenter in these controls: RecognizesAccessKey = true.

    So when ContentPresenter gets a string as the content of the control and this RecognizesAccessKey property is set to true, it will generate an AccessText instead of the standard TextBlock.

    You’re issue: you effectively want to set properties on this generated AccessText. In this case there are two levels or indirection here: first, you have the Control to ContentPresenter indirection. You also have the ContentPresenter to AccessText indirection.

    The proposed solution is half-way right. Instead of adding a TextBlock directly to the Label (which doesn’t know anything about mnemonics) add an AccessText and set TextWrapping or TextTrimming or whatever you want and you should be set.

    Sunday, February 12, 2006 7:24 PM

All replies

  • Label doesn't support text wrapping, but TextFlow does:
    <TextFlow><Paragraph>Blah Blah Blah Blah Blah Blah Blah Blah </Paragraph></TextFlow>

    Sheva
    Saturday, February 11, 2006 5:19 PM
  • But you can cheat it by nesting the TextFlow element inside the Label element:
    <Label>
          <TextFlow>
            <Paragraph>
              Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah
              Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah
              Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah
              Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah
              Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah
            </Paragraph>
          </TextFlow>
        </Label>

    It works as you expected.
    Sheva
    Saturday, February 11, 2006 5:24 PM
  • Thanks for the response, footballism.

    Unfortunately, what you suggested does not meet my needs. Putting a TextFlow (or a TextBlock) inside a Label defeats the behavior that differentiates labels from other static text: the ability to underline one of the characters in the text as a mnemonic character, and the ability to specify a "Target" control that should get the focus when the user presses Alt+(the underlined character).

    This example underlines the "N" in "Name" and puts focus in the text box when the user types Alt+N:

    <Label Target="{Binding ElementName=m_textBox}">
      First _Name:
    </Label>
    <TextBox Name="m_textBox"/>

    But this example does not:

    <Label Target="{Binding ElementName=m_textBox}">
      <TextFlow>
        <Paragraph>

          First _Name:
        </Paragraph>
      </TextFlow>
    </Label>
    <TextBox Name="m_textBox"/>

    So it would still be useful to be able to say TextWrapping="Wrap" on a Label, as you can on a TextBlock.

    Sunday, February 12, 2006 2:15 AM
  • Ah! Here's the story with mnemonics:
    AccessText is the control that does the work to parse the "_" and register the mnemonic.
    You might ask: where’s the AccessText in Button, Label, Menu, etc?

    The answer: it’s generated.

    The templates for all of these controls have a ContentPresenter. An additional property is set on the ContentPresenter in these controls: RecognizesAccessKey = true.

    So when ContentPresenter gets a string as the content of the control and this RecognizesAccessKey property is set to true, it will generate an AccessText instead of the standard TextBlock.

    You’re issue: you effectively want to set properties on this generated AccessText. In this case there are two levels or indirection here: first, you have the Control to ContentPresenter indirection. You also have the ContentPresenter to AccessText indirection.

    The proposed solution is half-way right. Instead of adding a TextBlock directly to the Label (which doesn’t know anything about mnemonics) add an AccessText and set TextWrapping or TextTrimming or whatever you want and you should be set.

    Sunday, February 12, 2006 7:24 PM
  • Thanks, Kevin! That's the answer I needed.

    For followup, could you please get this documented in the "Label Class: about Label Class" documentation section? I suspect it's a question that will come up a lot.

    Monday, February 13, 2006 5:27 AM
  • This has been flagged to our SDK folks.

    Thanks for the feedback.

    Monday, February 13, 2006 7:33 PM
  • Thanks.

    Or better yet, if it makes sense in your object model, go ahead and expose properties like TextWrapping to the Label class itself. The template could pull those in to the inner AccessText.

    Monday, February 13, 2006 7:37 PM