【发布时间】:2019-12-13 09:58:11
【问题描述】:
我试图在工具提示中显示数据网格行的验证错误消息。这是当我直接使用 Grid 控件的 ToolTip 属性时的代码(没有样式):
<Grid Margin="0,-2,0,-2" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}">
<Ellipse StrokeThickness="0" Fill="Red"
Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White"
HorizontalAlignment="Center" />
</Grid>
现在这会正确显示工具提示。一切都很好。
问题:
一旦我开始单独设置工具提示的样式,绑定就会以某种方式停止工作。这是我正在尝试但不起作用的代码:
<Grid Margin="0,-2,0,-2">
<Ellipse x:Name="ErrorEllipse" StrokeThickness="0" Fill="Red"
Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White"
HorizontalAlignment="Center"/>
<Grid.ToolTip>
<ToolTip Background="Transparent" BorderBrush="Transparent" Height="Auto" Width="Auto">
<Border >
<TextBlock Text= "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}"
MinHeight="20"
MinWidth="100"
Foreground="White"
Background="Red"/>
</Border>
</ToolTip>
</Grid.ToolTip>
</Grid>
我在这里缺少什么以及如何实现正确的绑定?可能是一些基本的东西,但我不知道......
【问题讨论】:
-
不是样式是你的问题。工具提示不在可视树中。这是一个弹出窗口。因此,您的 relativesource 将找不到 datagridrow。您需要一个位于可视化树中的资源并获取该资源或放置目标。这是对您的问题weblogs.asp.net/monikadyrda/wpf-tooltip-content-binding 的一个超级简单的解释,顺便说一句,您应该用谷歌搜索。还。 [0] 改用 / 不会出现错误。
标签: wpf data-binding datagrid