Skip to main content

Microsoft Silverlight

Answered Question Does DataBinding works in ToolTip when defining tooltip layout ?RSS Feed

(0)

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

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.

Sergey.Lutay
Sergey.L...

Contributor

Contributor

7094 points

1,333 Posts

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!)

Blog

Twitter

Sincerely,
Sergey Lutay

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

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.

lee_sl
lee_sl

Contributor

Contributor

2990 points

584 Posts

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/

Sergey.Lutay
Sergey.L...

Contributor

Contributor

7094 points

1,333 Posts

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!)

Blog

Twitter

Sincerely,
Sergey Lutay

silverbyte
silverbyte

Participant

Participant

1338 points

405 Posts

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
silverbyte

Participant

Participant

1338 points

405 Posts

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
silverbyte

Participant

Participant

1338 points

405 Posts

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
silverbyte

Participant

Participant

1338 points

405 Posts

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
silverbyte

Participant

Participant

1338 points

405 Posts

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.

Amanda Wang - MSFT
Amanda W...

All-Star

All-Star

17241 points

1,466 Posts

Answered Question

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.

JohnDMathis
JohnDMathis

Member

Member

9 points

15 Posts

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?

maruzounaki
maruzounaki

Member

Member

36 points

114 Posts

Re: Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

 This is my codes and i still cant get the bindings to work from my tooltip and also eventsdetailview. can anyone help me on this?

Page.xaml.cs

void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Stadium.Click += new RoutedEventHandler(Stadium_Click);
        }

        void Stadium_Click(object sender, RoutedEventArgs e)
        {
            TPmap.ServiceReference1.Eventp c = new TPmap.ServiceReference1.Eventp();
            if (c != null)
            {
                DetailsView.DataContext = c;
                DetailsView.Visibility = Visibility.Visible;
            }
            
        }

        void cs_GetEventsByPlaceCompleted(object sender, TPmap.ServiceReference1.GetEventsByPlaceCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}

Eventp.cs

namespace TPmap.Web
{
    public class Eventp
    {
        public string Images { get; set; }
        public string Events { get; set; }
        public string Places { get; set; }
    }
}

EventService.svc.cs

namespace TPmap.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class EventService
    {
        [OperationContract]
        public List<Eventp> GetEventsByPlace(string place)
        {
            List<Eventp> clist = new List<Eventp>();
            string connectionString = @"Server=IFCDU2-WS03\SQLEXPRESS; Database=TP; UID=user; PWD=; Trusted_Connection=True";

            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            string cmd = "Select * from Events";

            SqlCommand com = new SqlCommand(cmd, con);
            SqlDataReader r = com.ExecuteReader();
            while (r.Read())
            {
                Eventp c = new Eventp();
                c.Images = r["image"].ToString();
                c.Events = r["eventName"].ToString();
                c.Places = r["place"].ToString();
                clist.Add(c);
            }
            com.Dispose();

            con.Close();
            con.Dispose();

            return clist;
        }
    }
}

Page.xaml

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="TPmap.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:TPmap="clr-namespace:TPmap">

<HyperlinkButton Height="56" HorizontalAlignment="Left" Margin="72.5,172.036,0,0" x:Name="Stadium" VerticalAlignment="Top" Width="49" Content="Stadium" Click="Stadium_Click">
            <ToolTipService.ToolTip>
                <StackPanel>
                    <TextBlock Text="{Binding Events}"/>
                    <Image Source="{Binding Images}" Width="600" Height="80"/>
                    <TextBlock TextAlignment="Center" Text="Temasek Polytechnic Stadium"/>
                </StackPanel>
            </ToolTipService.ToolTip>
        </HyperlinkButton>

 this is what ive come out with but still, there isnt anything displayed inside my tooltip.Help anyone?

Andre Michaud
Andre Mi...

Member

Member

212 points

31 Posts

Microsoft

Re: Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

 Is this with silverlight 2 or 3? I believe that this was a known issue with silverlight 2 that was fixed in 3 (datacontext doesn't pass through a tooltip).

 

remember - please mark answered posts as such

maruzounaki
maruzounaki

Member

Member

36 points

114 Posts

Re: Re: Re: Re: Re: Does DataBinding works in ToolTip when defining tooltip layout ?

 im still using Silverlight 2. cause my project is using silverlight 2. :S

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities