我将 System.Windows.Style 作为 DataGrid 中标题的工具提示文本
我想为数据网格列定义一种样式,通过附加属性获取工具提示的文本。但我得到的是文本 System.Windows.Style
而不是文本。
代码是这样的。定义样式的XML资源文件:
<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DataGridColumnHeaderConTooltip">
<Setter Property="ToolTip">
<Setter.Value>
<Style TargetType="ToolTip">
<Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<!--Para poder utilizar el attached propery, se tiene que utilizar PlacementTarget, y además indicar que el source
es el control padre, que es el tooltip, porque el TextBlck no pertenece al mismo visual tree.-->
<TextBlock Text="{Binding PlacementTarget.(ap:CabeceraDatagridAttachedProperty.Tooltip), RelativeSource={RelativeSource AncestorType=ToolTip}}" MaxWidth="400" TextWrapping='Wrap' />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
xaml中的代码:
<DataGridTextColumn Header="Cantidad Para Descontar" Binding="{Binding CantidadParaDescontar, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnDataErrors=True}" Width="AUTO" IsReadOnly="false"
ap:CabeceraDatagridAttachedProperty.Tooltip="tooltip cabecera por attached property"
HeaderStyle="{StaticResource DataGridColumnHeaderConTooltip}">
附加属性:
namespace GTS.CMMS.Client.AttachedProperties
{
public static class CabeceraDatagridAttachedProperty
{
public static readonly DependencyProperty TooltipProperty =
DependencyProperty.RegisterAttached(
"Tooltip",
typeof(string),
typeof(CabeceraDatagridAttachedProperty));
public static string GetTooltip(DependencyObject obj)
{
return (string)obj.GetValue(TooltipProperty);
}
public static void SetTooltip(DependencyObject obj, string value)
{
obj.SetValue(TooltipProperty, value);
}
}
}
I want to define a style for the datagrid columns that get the text of the tooltip through an attached property. But I get the text System.Windows.Style
instead of the text.
The code is this. XML resource file that defines the style:
<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DataGridColumnHeaderConTooltip">
<Setter Property="ToolTip">
<Setter.Value>
<Style TargetType="ToolTip">
<Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<!--Para poder utilizar el attached propery, se tiene que utilizar PlacementTarget, y además indicar que el source
es el control padre, que es el tooltip, porque el TextBlck no pertenece al mismo visual tree.-->
<TextBlock Text="{Binding PlacementTarget.(ap:CabeceraDatagridAttachedProperty.Tooltip), RelativeSource={RelativeSource AncestorType=ToolTip}}" MaxWidth="400" TextWrapping='Wrap' />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
The code in the xaml:
<DataGridTextColumn Header="Cantidad Para Descontar" Binding="{Binding CantidadParaDescontar, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnDataErrors=True}" Width="AUTO" IsReadOnly="false"
ap:CabeceraDatagridAttachedProperty.Tooltip="tooltip cabecera por attached property"
HeaderStyle="{StaticResource DataGridColumnHeaderConTooltip}">
The attached property:
namespace GTS.CMMS.Client.AttachedProperties
{
public static class CabeceraDatagridAttachedProperty
{
public static readonly DependencyProperty TooltipProperty =
DependencyProperty.RegisterAttached(
"Tooltip",
typeof(string),
typeof(CabeceraDatagridAttachedProperty));
public static string GetTooltip(DependencyObject obj)
{
return (string)obj.GetValue(TooltipProperty);
}
public static void SetTooltip(DependencyObject obj, string value)
{
obj.SetValue(TooltipProperty, value);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是预期的,因为您为
ToolTip
属性分配了样式而不是内容。ToolTip
不知道如何显示Style
,因此它调用ToString()
。您应该做的是将所需的附加属性直接绑定到
ToolTip
属性。使用Self
作为RelativeSource
来引用底层的DataGridColumnHeader
。然后导航到其Column
属性并指定您的附加属性。This is expected, since you assigned a style to the
ToolTip
property instead of content. TheToolTip
does not have any idea how to display aStyle
, so it callsToString()
.What you should do is bind the desired attached property directly to the
ToolTip
property. UseSelf
asRelativeSource
to refer to the underlyingDataGridColumnHeader
. Then navigate to itsColumn
property and specify your attached property.