Archive
Customizing tooltip
This post shows how to customize the tooltip in silverlight toolkit charts
we need to add references to Microsoft.Windows.Controls.DataVisualization as well as Microsoft.Windows.Controls.DataVisualization dll’s
Grab the Template from the toolkit source
the only change I made was to replace the Tooltip content
original
<ToolTipService.ToolTip>
<StackPanel>
<ContentControl Content=”{TemplateBinding FormattedDependentValue}” />
<ContentControl Content=”{TemplateBinding FormattedRatio}” />
</StackPanel>
</ToolTipService.ToolTip>
modified
<ToolTipService.ToolTip>
<StackPanel Orientation=”Horizontal”>
<ContentControl Content=”{TemplateBinding IndependentValue }” />
<TextBlock Text=” – “></TextBlock>
<ContentControl Content=”{TemplateBinding FormattedDependentValue}” />
<TextBlock Text=” – “></TextBlock>
<ContentControl Content=”{TemplateBinding FormattedRatio}” />
</StackPanel>
</ToolTipService.ToolTip>
The chart itself is declared along with the style as following
<charting:Chart x:Name=”chart1″ >
<charting:Chart.StylePalette>
<datavis:StylePalette>
<Style TargetType=”charting:PieDataPoint”>
<Setter Property=”Background”
Value=”blue” />
<Setter Property=”Template”
Value=”{StaticResource PieDataPointTemplate}” />
</Style>
<Style TargetType=”charting:PieDataPoint”>
<Setter Property=”Background”
Value=”red” />
<Setter Property=”Template”
Value=”{StaticResource PieDataPointTemplate}” />
</Style>
<Style TargetType=”charting:PieDataPoint”>
<Setter Property=”Background”
Value=”green” />
<Setter Property=”Template”
Value=”{StaticResource PieDataPointTemplate}” />
</Style>
<Style TargetType=”charting:PieDataPoint”>
<Setter Property=”Background”
Value=”yellow” />
<Setter Property=”Template”
Value=”{StaticResource PieDataPointTemplate}” />
</Style>
</datavis:StylePalette>
</charting:Chart.StylePalette>
<charting:Chart.Series>
<charting:PieSeries IndependentValueBinding=”{Binding Key}”
DependentValueBinding=”{Binding Path=Value}”>
</charting:PieSeries>
</charting:Chart.Series>
</charting:Chart>
you can download the source here
DataGridRow and Tooltip
We can use Popup to display a tooltip at the row level in the datagrid
Here is sample code
<UserControl x:Class=”slapp1a.Page”
xmlns=”http://schemas.microsoft.com/client/2007“
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml“
xmlns:data=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”
Width=”600″ Height=”600″>
<UserControl.Resources>
<DataTemplate x:Key=”dt”>
<StackPanel Orientation=”Horizontal”>
<TextBlock Text=”This is a tooltip for the Row with content : “></TextBlock>
<TextBlock Text=”{Binding}”></TextBlock>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name=”LayoutRoot” Background=”White”>
<data:DataGrid x:Name=”dataGrid1″
Height=”400″
Width=”450″
Margin=”0,5,0,10″
AutoGenerateColumns=”True” />
<Popup x:Name=”myPopup”>
<ContentControl x:Name=”PopupContent” ContentTemplate=”{StaticResource dt}”></ContentControl>
</Popup>
</Grid>
</UserControl>
and in codebehind. all we are doing is handling the PreparingRow event to attach evenhandlers for MouseEvents, where we position the popup and set the content for the contentcontrol in the popup.
Did not try it, but we could change it use storyboards to introduce some delay before showing the tooltip and ofcourse customize the appearance
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
dataGrid1.ItemsSource = new string[] { “data 1″, “data 2″ };
dataGrid1.PreparingRow += new EventHandler<DataGridRowEventArgs>(dataGrid1_PreparingRow);
} void dataGrid1_PreparingRow(object sender, DataGridRowEventArgs e)
{
e.Row.MouseEnter += new MouseEventHandler(Row_MouseEnter);
e.Row.MouseLeave += new MouseEventHandler(Row_MouseLeave);
}
void Row_MouseLeave(object sender, MouseEventArgs e)
{
myPopup.IsOpen = false;
}
void Row_MouseEnter(object sender, MouseEventArgs e)
{
PopupContent.Content = (sender as DataGridRow).DataContext;
Point p = e.GetPosition(null);
myPopup.HorizontalOffset = p.X;
myPopup.VerticalOffset = p.Y;
myPopup.IsOpen = true;
}
}

