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.

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

How would I put rich text into this object and how would I then bind to this object.
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.
I really need to have quite a complicated ToolTip and I don't think that would work anyway.
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.
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.
0

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}"/>

Comments

0

If your ToolTip text (for example) is not something you can know at design time, but must build in code-behind at runtime, then you can do it like this:

TextBlock tb = new TextBlock();
tb.Inlines.Add(new Run("Background indicates packet repeat status:"));
tb.Inlines.Add(new LineBreak());
tb.Inlines.Add(new LineBreak());
Run r = new Run("White");
r.Background = Brushes.White;
tb.Inlines.Add(r);
tb.Inlines.Add(new Run("\t= Identical Packet received at this time."));
tb.Inlines.Add(new LineBreak());
r = new Run("SkyBlue");
r.Background = new SolidColorBrush(Colors.SkyBlue);
tb.Inlines.Add(r);
tb.Inlines.Add(new Run("\t= Original Packet received at this time."));

ToolTip tt = new ToolTip();
tt.Content = tb;
myButton.ToolTip = tt;

This example gives a nicely formatted 4-line tool tip text, with color highlights.

The only reason I didn't use Brushes.SkyBlue was to demonstrate that you can build any color you want. E.g., new SolidColorBrush(Color.FromArgb(128, 86, 180, 233)); for a half-transparent SkyBlue background.

When choosing colors, remember your color-blind users: Color Blind Palette

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.