Reputation: 123
I have a datagrid control in which some columns contain text overflowing the respective containers.
To solve this problem I add tooltip into the controltemplate for datagridcell, then bind the content of tooltip to the data content by templatebinding to Content property.
The outlined Xaml code is like below:
<Style x:Key="RotatedCell" TargetType="sdk:DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:DataGridCell">
<ContentPresenter
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}" >
<ToolTipService.ToolTip>
<ToolTip Content="{TemplateBinding Content}"/>
</ToolTipService.ToolTip>
</ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The datagrid can be show with data displayed successfully, but when the cursor hovers over
column error is reported:
*
Microsoft JScript runtime error: Unhandled Error in Silverlight Application [Line: 0 Position: 0]
at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
at MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name)
at MS.Internal.XcpImports.FrameworkElement_ApplyTemplate(FrameworkElement frameworkElement)
at System.Windows.Controls.Control.ApplyTemplate()
at System.Windows.Controls.ToolTip.OpenPopup()
at System.Windows.Controls.ToolTip.OnIsOpenChanged(Boolean isOpen)
at System.Windows.Controls.ToolTip.OnIsOpenPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
at System.Windows.DependencyObject.SetValue(DependencyProperty property, Boolean b)
at System.Windows.Controls.ToolTipService.OpenAutomaticToolTip(Object sender, EventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
This phenomenon puzzles me, because i think the xaml syntax seems correct and no errors are reported during building and loading processes. It appears like the tooltip control tries to apply template when about to show up, but I think that should be done in the same time as the datagridcell control template was applied. Anybody has any ideas about this?
Upvotes: 1
Views: 914
Reputation: 168
Try this:
<Setter Property="ToolTip" Value={Binding Content} />
UPDATE: Yep, I forgot that you asked about Silverlight and not WPF. This will work nice, I checked:
<Style x:Key="DataGridCellStyle"
TargetType="sdk:DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:DataGridCell">
<Grid x:Name="Root"
Background="{TemplateBinding Background}">
<ToolTipService.ToolTip>
<ContentControl Content="{TemplateBinding Content}" />
</ToolTipService.ToolTip>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Upvotes: 0