none
How to add *formatted* text as a DependencyProperty's type and bind it to a TextControl's content?

    Question

  • I have created a UserControl. Its XAML contains a TextBlock to represent a Description property displayed on the control's surface.

    I need advice on how to turn this description into a dependency property of the class.

    The hard part is that the description may contain formatting, such as <Underline>, <LineBreak> etc. So I can't just use a string property and bind it to the TextBlock.Text property.

    What is the best data type for that property to enable this scenario?

    NB: I have been able to "solve" this problem crudely, and unsatisfactorily, by creating a DependencyProperty (called Description) in the codebehind class with a data type of TextBlock. The xaml for the UserControl then contains the following markup:


    <UserControl Name="myUserControl"

    ...

    <TextBlock Name="descriptionTextBlock" Width="85" Height="52" Canvas.Left="65" Canvas.Top="32" TextWrapping="Wrap" >

      <ContentPresenter>

        <ContentPresenter.Content>

          <Binding ElementName="myUserControl" Path="Description" />

        </ContentPresenter.Content>

      </ContentPresenter>

    </TextBlock>

    ...


    In Page1.xaml I then have some markup like this:

    <c:myUserControl>

      <c:myUserControl.Description>

        <TextBlock>sample <Underline>formatted</Underline> description<LineBreak/>

          <TextBlock FontWeight="Light">a nested text block</TextBlock>

        </TextBlock>

      </c:myUserControl.Description>

    </c:myUserControl>


    This is still not entirely satisfactory though, because:

    1. The TextBlock inserted from Page1.xaml doesn't inherit descriptionTextBlock's properties, like Width, Height and TextWrapping. So it doesn't position itself correctly within the user control.

    2. This forces one to create a TextBlock in Page1.xaml. It would be nice to be able to do the following when formatting is not required:

    <c:myUserControl  Description="An unformatted description" />


    Surely there must be a better way! Anyone have any suggestions?

    Friday, April 21, 2006 12:14 PM

Answers

  • Just off the top of my head:

    1. Instead of having a TextBlock inside your user control, just put a ContentControl
    2. Make your user control's dependency property type Object
    3. Bind your ContentControl's Content property to the dependency property and let it handle the rest.

    If it's a string, ContentControl will turn it into a TextBlock. If it's anything else, it should inject it into the logical tree for you as is.

    HTH,
    Drew

    Friday, April 21, 2006 5:46 PM
    Moderator

All replies

  • Just off the top of my head:

    1. Instead of having a TextBlock inside your user control, just put a ContentControl
    2. Make your user control's dependency property type Object
    3. Bind your ContentControl's Content property to the dependency property and let it handle the rest.

    If it's a string, ContentControl will turn it into a TextBlock. If it's anything else, it should inject it into the logical tree for you as is.

    HTH,
    Drew

    Friday, April 21, 2006 5:46 PM
    Moderator
  • Thanks, Drew. That works very nicely!

    The only slight problem remaining is that it isn't strongly typed - but I can live with that, as I will be the one instantiating the control.

    Tuesday, May 02, 2006 11:51 AM