Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Does DataBinding works in ToolTip when defining tooltip layout ?
14 replies. Latest Post by maruzounaki on July 30, 2009.
(0)
silverbyte
Participant
1338 points
405 Posts
11-19-2008 6:10 AM |
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 ?
Sergey.L...
Contributor
7094 points
1,333 Posts
11-19-2008 6:13 AM |
Hi,
Can you show how you set an data item to your element?
11-19-2008 6:24 AM |
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.
lee_sl
2990 points
584 Posts
11-19-2008 6:31 AM |
you have to handle loaded event of the control and set the tooltip programatically
11-19-2008 6:33 AM |
I think you need to set DataContext property of TextBlock.
11-19-2008 6:59 AM |
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>
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 ?
11-19-2008 7:14 AM |
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.
11-19-2008 9:46 AM |
anyone ?
11-19-2008 5:23 PM |
would be nice to have an explanation
11-20-2008 10:56 AM |
anybody :)
Amanda W...
All-Star
17241 points
1,466 Posts
11-24-2008 5:40 AM |
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; }
JohnDMathis
Member
9 points
15 Posts
03-13-2009 2:37 PM |
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
36 points
114 Posts
07-27-2009 9:36 PM |
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>
Andre Mi...
212 points
31 Posts
07-29-2009 1:39 PM |
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).
07-30-2009 5:37 AM |
im still using Silverlight 2. cause my project is using silverlight 2. :S