8

I want to make a tooltip with multibinding inside a text block, but whatever I try it doesn't work.

Here is what I've tried so far:

<TextBlock Text="{Binding Description, StringFormat='Description : {0}{}'}">
    <ToolTipService.ToolTip>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="Description : {0}{1}{}">
                    <Binding Path="FirstDescription" />
                    <Binding Path="SecondDescription" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </ToolTipService.ToolTip>
</TextBlock>

But when I try it, what I see on the tooltip is : System.Windows.Controls.TextBlock.

when i try it without tooltipservice, and only tooltip, like this :

<TextBlock Text="{Binding Description, StringFormat='Description : {0}{}'}">
    <ToolTip>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="Description : {0}{1}{}">
                    <Binding Path="FirstDescription" />
                    <Binding Path="SecondDescription" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </ToolTip>
</TextBlock>

The screen just get stuck.

Golan Kiviti
  • 3,895
  • 7
  • 38
  • 63

3 Answers3

5

I dont't know wich VS version you are using but:

<TextBlock Text="{Binding Description, StringFormat="Description : {0}{}"}">

does not even compile for me.

Just remove the " and the empty brackets like that:

<TextBlock Text="{Binding Description, StringFormat=Description : {0}">

You could also write it like this if you want the ":

<TextBlock>
    <TextBlock.Text>
        <Binding Path="Description" StringFormat="Description : {0}" />
    </TextBlock.Text>
    <ToolTipService.ToolTip>
        <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="Description : {0}{1}">
                            <Binding Path="FirstDescription" />
                            <Binding Path="SecondDescription" />
                        </MultiBinding>
                    </TextBlock.Text>
        </TextBlock>
    </ToolTipService.ToolTip>
</TextBlock>
Ouarzy
  • 3,015
  • 1
  • 16
  • 19
  • Please also remove the empty bracket, just to be sure. Then as I said, you may have a problem in how you fill your binding, it should work. It it displays "System.Windows.Controls.TextBlock" it could mean that you code something like Description = new TextBlock() in your code behind? – Ouarzy Dec 29 '15 at 10:41
1

I have tried the following code and that worked perfectly:

<TextBlock Margin="20" Foreground="Black" FontSize="20" FontFamily="Century Gothic" Text="{Binding Name1}">
        <TextBlock.ToolTip>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="MultiBinded Tooltip : {0}{1}">
                        <Binding Path="Name1"/>
                        <Binding Path="Name2"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </TextBlock.ToolTip>
    </TextBlock>
0

Just delete empty brackets. Next code work as expected:

<TextBlock Text="{Binding Description, StringFormat='Description : {0}'}">
    <ToolTipService.ToolTip>
        <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="Description : {0}{1}">
                            <Binding Path="FirstDescription" />
                            <Binding Path="SecondDescription" />
                        </MultiBinding>
                    </TextBlock.Text>
        </TextBlock>
    </ToolTipService.ToolTip>
</TextBlock>

If the StringFormat starts with a left brace { the XAML parser require you to escape it using a pair of braces {}. Otherwise the parser gets confused because braces also are used in the syntax of markup extensions.

Details are found in the XAML documentation for {} Escape Sequence / Markup Extension.

Also, you can't use double quotes with inline binding but single quotes is available.

Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
  • @Pachu ok I answered you about brackets and add whole workong code. Did you still have any problems after using it? – Vadim Martynov Dec 29 '15 at 10:12
  • I do, I see the following text in the tool tip : "System.Windows.Controls.TextBlock" instead of the text I want. – Golan Kiviti Dec 29 '15 at 10:17
  • Sorry but I can't reproduce your problem. I deleted empty brackets and now code works fine. What version of .net framework do you use? Is it your example is a part of Style/Template or not? – Vadim Martynov Dec 29 '15 at 10:27
  • .net 4.5, and yes it is a part of a data template – Golan Kiviti Dec 29 '15 at 10:32
  • My guess is that "System.Windows.Controls.TextBlock" is what is actually binded in your tooltip. What is your code behind to fill Description, FirstDescription and SecondDescription? – Ouarzy Dec 29 '15 at 10:39
  • Also you can't bind directly to your viewmodel inside datatemplate. See more [on SO](http://stackoverflow.com/questions/18245936/binding-to-viewmodel-from-inside-a-datatemplate) – Vadim Martynov Dec 29 '15 at 10:58