2

DataGridRow 的默认样式如下:

<Style x:Key="{x:Type DataGridRow}" TargetType="{x:Type DataGridRow}">
  <Setter Property="Background" Value="{DynamicResource {x:Static 
                                      SystemColors.WindowBrushKey}}" />
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
    <Setter Property="ValidationErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <TextBlock Margin="2,0,0,0" VerticalAlignment="Center" 
                                    Foreground="Red" Text="!" />
      </ControlTemplate>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
      ... 
  </Setter>
</Style>

我想要的是将 ToolTip 添加到显示“!”的 TextBlock 在行标题中,它将从 DataGridRow.Item.Error 属性中获取错误消息(我的实体对象实现了 IDataErrorInfo)。所以我做了以下事情:

<TextBlock Margin="2,0,0,0" VerticalAlignment="Center"
                            Foreground="Red" Text="!"
           ToolTip="{Binding RelativeSource={
                             RelativeSource FindAncestor,
                             AncestorType={x:Type DataGridRow}},
                             Path=Item.Error}"/>

到现在为止还挺好。现在,Error 属性返回多行字符串,所以我想将 TextBlock 用于 ToolTip:

<TextBlock Margin="2,0,0,0" VerticalAlignment="Center"
                            Foreground="Red" Text="!">
  <TextBlock.ToolTip >
    <TextBlock Text="{Binding RelativeSource={
                              RelativeSource FindAncestor,
                              AncestorType={x:Type DataGridRow}},
                              Path=Item.Error}"
               TextWrapping="Wrap"/>
  </TextBlock.ToolTip>
</TextBlock>

但以上不会显示错误信息;问题似乎是 ToolTip 不是父元素的可视化树的一部分。我读过 PlacementTarget 等,但仍然无法完成工作。有人可以告诉我执行上述操作的正确方法吗?

4

2 回答 2

2

I think the problem is to bind to relative source of the given element's property (PlacementTarget), not the element itself. But RelativeSource markup extension describes the location of the binding source relative to the given element. So, what I did was to set PlacementTarget to the ancestor of original tooltip target:

<Setter Property="ValidationErrorTemplate">
  <Setter.Value>
    <ControlTemplate>
      <TextBlock Margin="2,0,0,0" VerticalAlignment="Center"
                 HorizontalAlignment="Center"
                 TextAlignment="Center"
                 Foreground="Red" Text="!"
                 ToolTipService.PlacementTarget="{Binding RelativeSource={
                              RelativeSource FindAncestor,
                              AncestorType={x:Type DataGridRow}}}">
        <TextBlock.ToolTip >
          <ToolTip DataContext="{Binding Path=PlacementTarget, 
                   RelativeSource={x:Static RelativeSource.Self}}">
            <TextBlock Text="{Binding Path=Item.Error}"/>
          </ToolTip>
        </TextBlock.ToolTip>
      </TextBlock>
    </ControlTemplate>
  </Setter.Value>
</Setter>

Now it works as I wanted.

于 2013-01-19T10:48:27.660 回答
0

使用该文本块中的祖先级别,如下所示

          <TextBlock Text="{Binding RelativeSource={
                          RelativeSource FindAncestor,
                          AncestorType={x:Type DataGridRow},AncestorLevel=1},
                          Path=Item.Error}"
           TextWrapping="Wrap"/>
于 2013-01-18T11:55:05.393 回答