Page view counter
Does DataBinding works in ToolTip when defining tooltip layout ? Subscribe to this thread
Last post 03-13-2009 2:37 PM by JohnDMathis. 11 replies.
Sort Posts:
11-19-2008 6:10 AM
Does DataBinding works in ToolTip when defining tooltip layout ?

 I am trying to create my own ToolTip layout like this:

                            <ToolTipService.ToolTip>
                                <StackPanel>
                                    <TextBlock Text="{Binding Title}" />
                                </StackPanel>
                            </ToolTipService.ToolTip>

 But it doesn't seem to work, it displays a small part of a tooltip (a small horizontal line), it appears like the binding does not work and tooltip text is empty "".

If I replace with a text instead (no data binding), obviously, it works:

                             <ToolTipService.ToolTip>
                                <StackPanel>
                                    <TextBlock Text="AAA" />
                                </StackPanel>
                            </ToolTipService.ToolTip>

 

 Furthermore, if I use the ToolTipService.ToolTip as an attached property it works:     

ToolTipService.ToolTip="{Binding Title}".

So the binding does work.

It seems that the tooltip doesn't work when I am trying to define the layout AND use data binding

Have you used tooltip with layout and data binding successfully ?

 

Please click on 'Mark as answer' near my comment if you feel I answered your question.

silverbyte

Loading...
Joined on 07-03-2008
Posts 385
11-19-2008 6:13 AM
Re: Does DataBinding works in ToolTip when defining tooltip layout ?

Hi,

Can you show how you set an data item to your element?

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

My blog

Twitter

Sincerely,
Sergey Lutay

Sergey.Lutay

Loading...
Joined on 02-14-2008
Ukraine
Posts 386
11-19-2008 6:24 AM
Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

 Yes, thanks for your comment.

I've made a simple test application. I created a new Silverlight application and added a Button on the Page:

 

<UserControl x:Class="TooltipDataBindingTest.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button x:Name="btn" Content="Caption" Width="100" Height="30" ToolTipService.ToolTip="{Binding Name}">
            <!-- The following does NOT work:-->
            <!--
            <ToolTipService.ToolTip>
                <TextBlock Text="{Binding Name}" />
            </ToolTipService.ToolTip> -->
        </Button>
    </Grid>
</UserControl>

Binding is defined like this:

 

    public class Person
    {
        public string Name
        {
            get;
            set;
        }
    } 

namespace TooltipDataBindingTest
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            Person person = new Person();
            person.Name = "Andrew";
            this.btn.DataContext = person;
        }
    }
}

 

 

You can quickly create a Silvelright application called TooltipDataBindingTest so you can copy and paste the above code.


Please click on 'Mark as answer' near my comment if you feel I answered your question.

silverbyte

Loading...
Joined on 07-03-2008
Posts 385
11-19-2008 6:31 AM
Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

you have to handle loaded event of the control and set the tooltip programatically

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

lee_sl

Loading...
Joined on 05-09-2008
Posts 549
11-19-2008 6:33 AM
Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

I think you need to set DataContext property of TextBlock.

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

My blog

Twitter

Sincerely,
Sergey Lutay

Sergey.Lutay

Loading...
Joined on 02-14-2008
Ukraine
Posts 386
11-19-2008 6:59 AM
Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

 Thank you Sergey.

  Indeed, setting the DataContext on the TextBlock make the tooltip to work:

       <Button x:Name="btn" Content="Caption" Width="100" Height="30">
            <ToolTipService.ToolTip>
                <TextBlock x:Name="txtBlock" Text="{Binding Name}"/>
            </ToolTipService.ToolTip>
        </Button>

 

namespace TooltipDataBindingTest
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            Person person = new Person();
            person.Name = "Andrew";
            this.txtBlock.DataContext = person;
        }
    }
}


But I still don't understand and I am confused.

 Why when using ToolTipService.ToolTip attached property it DOES work:

<UserControl x:Class="TooltipDataBindingTest.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button x:Name="btn" Content="Caption" Width="100" Height="30" ToolTipService.ToolTip="{Binding Name}" />
    </Grid>
</UserControl>

 

but when using a custom layout it does NOT work:

<UserControl x:Class="TooltipDataBindingTest.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button x:Name="btn" Content="Caption" Width="100" Height="30">
            <ToolTipService.ToolTip>
                <TextBlock Text="{Binding Name}" />
            </ToolTipService.ToolTip>

        </Button>
    </Grid>
</UserControl>

 

Both of them use the same way of data binding:

    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            Person person = new Person();
            person.Name = "Andrew";
            this.btn.DataContext = person;
        }
    }

 

I expect either both to work or not work in the same time. Why they are working differently ?

 

Please click on 'Mark as answer' near my comment if you feel I answered your question.

silverbyte

Loading...
Joined on 07-03-2008
Posts 385
11-19-2008 7:14 AM
Re: Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

 I realize now that the fact that it doesn't work with custom layout has a great impact.

For example, I have a ListBox with ItemSource set.

In order for me to make the tooltip to work with a custom layout like above, what should I do, iterate through all items and set the DataContext for each tooltip TextBlock of an item ?

Again, I don't understand why using attached property works but not when setting a custom layout with ToolTip.Content.

 

Please click on 'Mark as answer' near my comment if you feel I answered your question.

silverbyte

Loading...
Joined on 07-03-2008
Posts 385
11-19-2008 9:46 AM
Re: Re: Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

 anyone ?

 

Please click on 'Mark as answer' near my comment if you feel I answered your question.

silverbyte

Loading...
Joined on 07-03-2008
Posts 385
11-19-2008 5:23 PM
Re: Re: Re: Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

 would be nice to have an explanation

Please click on 'Mark as answer' near my comment if you feel I answered your question.

silverbyte

Loading...
Joined on 07-03-2008
Posts 385
11-20-2008 10:56 AM
Re: Re: Re: Re: Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

 anybody :)

Please click on 'Mark as answer' near my comment if you feel I answered your question.

silverbyte

Loading...
Joined on 07-03-2008
Posts 385
11-24-2008 5:40 AM
Marked as Answer
Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

Hi silverbyte,

We have a debug on our labs, and found that the TextBlock's DataContext is null in ToolTipService.ToolTip .  That's why the button's tooltip is empty.

You can try to set the Textblock's datacontext in the code, like below code, the tooltip works fine on our local.

<Button x:Name="btn" Content="My Button" Width="100" Height="25" Click="btn_Click" >
            <ToolTipService.ToolTip>
                    <TextBlock x:Name="txt" Foreground="Red" Text="{Binding name}" Loaded="txt_Loaded"></TextBlock>
            </ToolTipService.ToolTip>
        </Button>

The code:

 private void txt_Loaded(object sender, RoutedEventArgs e)
        {
           TextBlock text = (TextBlock)sender;
            text.DataContext = this.btn.DataContext;
        }

 

Amanda Wang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Amanda Wang - MSFT

Loading...
Joined on 04-30-2007
Posts 1,015
03-13-2009 2:37 PM
Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

Unfortunately, the approach above does not seem to work when the TextBlock in question is inside a ControlTemplate.  (In my case, the ControlTemplate for a BarDataPoint.)  The Loaded event is never fired.  Can anyone suggest a suitable workaround?   Essentially, how can I get to a TextBlock defined in a ControlTemplate?

JohnDMathis

Loading...
Joined on 03-05-2009
Posts 3
Microsoft Communities