在 Silverlight DataGridTextColumn 上创建与绑定单元格内容不同的工具提示
我在 Silverlight 4 DataGrid
中有一个 DataGridTextColumn
,我想在该列上设置一个与绑定值不同的工具提示值。
我知道我可以很轻松地使用模板化列来完成此操作 - 但它添加了大量额外的 XAML,并且使得阅读和维护变得很麻烦。
这可行,但有很多额外的代码 - 特别是如果需要更改模板
<data:DataGridTemplateColumn Header="Name" Width="*">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextTrimming="WordEllipsis" Text="{Binding FullName}"
ToolTipService.ToolTip="{Binding Email}" Width="Auto" Margin="5" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
我想找到一种很好的方法来使用 Style 或继承的类来执行此操作。就像我说的,我的主要目标是以尽可能最好的方式减少 XAML 中像工具提示这样微不足道的东西的膨胀。
有一些类似的 stackoverflow 问题,其解决方案例如 this 和 这个,但它们都显示与单元格内容相同的工具提示值(对于溢出时的实例)。虽然这通常是您想要的 - 我正在尝试向单元格的内容显示不同的工具提示。
我确实找到了一些继承类的示例代码(滚动到末尾),我尝试修改它,但由于我的 XAML 知识达不到标准而卡住了,而且我不想在这上面花上一整夜!这个特定的示例看起来似乎有效,但它看起来像是一种黑客攻击,我认为尝试修改它以使用两个依赖属性将是一个更大的黑客攻击。
附言。我希望编写良好的子类可以让我轻松绑定其他属性,例如 TextTrimming。
I have a DataGridTextColumn
in a Silverlight 4 DataGrid
and I want to set a ToolTip value on the column that is different from the bound value.
I know I can do this with a templated column quite easily - but it adds a huge amount of extra XAML and makes it cumbersome to read and maintain.
This works, but is a lot of extra code - especially if ever need to change the template
<data:DataGridTemplateColumn Header="Name" Width="*">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock TextTrimming="WordEllipsis" Text="{Binding FullName}"
ToolTipService.ToolTip="{Binding Email}" Width="Auto" Margin="5" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
I'd like to find a nice way to do this with either a Style or inherited class. Like I said my main goal is to reduce bloat in the XAML for something so trivial as a tooltip in the best possible way.
There are a few similar stackoverflow questions with solutions like this and this, but they both show the same tooltip value as the contents of the cell (for instance when it overflows). While this is often what you want - I'm trying to show a different tooltip to the cell's contents.
I did find some sample code for an inherited class (scroll to end), which i tried to modify but got stuck becasue my XAML knowledge isn't up to par and I don't want to spend all night on this! This particular example appears like it works, but it looks like quite a hack and I think trying to modify it to work with two dependency properties is going to be an even bigger one.
PS. I would expect that a well written subclass would make it easy for me to bind other properties such as TextTrimming also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将此样式放入您的资源字典中:
要按照您在评论中的要求启用对任何属性的绑定,您需要发挥创意。我所做的是创建两个附加属性
ToolTipBinding
和IsToolTipBindingEnabled
。在列上设置ToolTipBinding
来确定工具提示,类似于确定单元格内容的Binding
属性,而IsToolTipBindingEnabled
为使用与我上面提到的类似的样式在DataGridCell
对象上设置为true
。然后,我编写了代码,以便在加载单元格时,其父列的绑定将应用于其
ToolTip
属性。下面是扩展类:
XAML 用法示例:
Try to put this style in your resource dictionary:
To enable binding to any property, as you requested in the comment, you need to get creative. What I did was create two attached properties
ToolTipBinding
andIsToolTipBindingEnabled
. TheToolTipBinding
is set on the column to determine the tooltip, similar to theBinding
property that determines the content of the cell, and theIsToolTipBindingEnabled
is set totrue
on theDataGridCell
object using a style similar to the one I mentioned above.Then, I wrote code so that when the cell is loaded, the binding from its parent column is applied to its
ToolTip
property.Here's the extension class:
Example XAML usage: