views:

625

answers:

2

I have a custom control which has a string Description dependancy property as shown below:

<CustomControl>
    <CustomControl.Description>
        Hello World
    </CustomControl.Description>
</CustomControl>

This description is bound in several places in TextBlock's as shown below:

<Button>
    <Button.ToolTip>
        <TextBlock Text="{Binding Path=Description}"/>
    </Button.ToolTip>
    <TextBlock Text="{Binding Path=Description}"/>
</Button>

How can I add new lines and bold formatting to the text blocks? I have tried:

  1. Adding \r\n to the description but this is not picked up.
  2. Adding &#x0a; or &#x0d;&#x0a; to the description but this is not picked up.
  3. Adding <![CDATA[<LineBreak/>]]> to the description but this is not picked up.
  4. Changing the type of the Description to a Label and using ContentPresenter controls to bind to the label but I found that only one ContentPresenter can bind to the Label at a time and the other keeps dissapearing.
  5. Changing the type of Description to a FlowDocument but I found that I could not add multiple viewer controls to view the same document.
+1  A: 

You might change your Description type from a simple string to a object thus allowing the container to set it to whatever it wants, rich text, images, etc.

The same framework element cannot be the logical child of two elements, therefore you can't use it as the content of the button and the tooltip at the same time.

Example:

<CustomControl>
    <CustomControl.Description>
        <WrapPanel>
            <TextBlock Text="Hello " Foreground="Red"/>
            <TextBlock Text="World!" Foreground="Blue"/>
        </WrapPanel>
    </CustomControl.Description>
</CustomControl>

Template:

<Button>
    <Button.ToolTip>
        <ContentPresenter Content="{TemplateBinding SomeOtherProperty}"/>
    </Button.ToolTip>
    <ContentPresenter Content="{TemplateBinding Description}"/>
</Button>
Aviad P.
How would I put rich text into this object and how would I then bind to this object.
anon
Example added...
Aviad P.
I have already done something similar. I made Description into a Label and displayed the label using ContentPresenter's. In the above example it would show in the button but when you hold your mouse over the button the ToolTip would show the description and the Button content would go missing. You only seem to be allowed one ContentPresenter per control.
anon
Try that (edited answer)
Aviad P.
I really need to have quite a complicated ToolTip and I don't think that would work anyway.
anon
I mistakenly wrote the tempalte in the style of a data template, when it should have been written in the style of a control template. Now it's updated.
Aviad P.
But you're right apparently, the same framework element cannot be the child of multiple parents, so it can either be the content of the button or the tooltip, not both.
Aviad P.
A: 

The type of Description should be DataTemplate:

    <DataTemplate>
        <TextBlock>
                <Run>Hello</Run>
                <LineBreak/>
                <Run>World</Run>
        </TextBlock>
    </DataTemplate>

You can then use labels and assign the template to each label.

<Label FontSize="24" ContentTemplate="{Binding Inlines}"/>
<Label FontSize="10" ContentTemplate="{Binding Inlines}"/>
anon