如何解决ToolTip无法模板绑定回datagridcell的内容
| 我有一个datagrid控件,其中某些列包含溢出相应容器的文本。 为了解决此问题,我将工具提示添加到datagridcell的控件模板中,然后 通过模板绑定到Content属性,将工具提示的内容绑定到数据内容。 概述的Xaml代码如下所示: <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>
可以显示数据网格并成功显示数据,但是当光标悬停在
报告列错误:
*
Microsoft JScript运行时错误:Silverlight应用程序中未处理的错误[行:0位置:0]
在MS.Internal.XcpImports.MethodEx(IntPtr ptr,字符串名称,CValue [] cvData)
在MS.Internal.XcpImports.MethodEx(DependencyObject obj,字符串名称)
在MS.Internal.XcpImports.FrameworkElement_ApplyTemplate(FrameworkElement frameworkElement)
在System.Windows.Controls.Control.ApplyTemplate()
在System.Windows.Controls.ToolTip.OpenPopup()
在System.Windows.Controls.ToolTip.OnIsOpenChanged(Boolean isOpen)
在System.Windows.Controls.ToolTip.OnIsOpenPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
在System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp,对象oldValue,对象newValue)
在System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty属性,EffectiveValueEntry oldEntry,EffectiveValueEntry&newEntry,ValueOperation操作)
在System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp,Object value,Boolean allowReadOnlySet)
在System.Windows.DependencyObject.SetValue(DependencyProperty属性,布尔b)
在System.Windows.Controls.ToolTipService.OpenAutomaticToolTip(Object sender,EventArgs e)
在MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,Delegate handlerDelegate,Object sender,Object args)
在MS.Internal.JoltHelper.FireEvent处(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex,字符串eventName)
*
这种现象使我感到困惑,因为我认为xaml语法似乎正确,并且在构建和加载过程中未报告任何错误。看起来像工具提示控件试图在要显示时应用模板,但是我认为应该在应用datagridcell控件模板的同时进行。有人对此有任何想法吗?
解决方法
尝试这个:
<Setter Property=\"ToolTip\" Value={Binding Content} />
更新:是的,我忘记了您问的是Silverlight而不是WPF。这会很好用,我检查了一下:
<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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。