【问题标题】:WPF DataGrid, showing errors in row header with ToolTipWPF DataGrid,使用工具提示在行标题中显示错误
【发布时间】:2013-01-18 11:17:20
【问题描述】:

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 等,但仍然无法完成工作。有人可以告诉我执行上述操作的正确方法吗?

【问题讨论】:

    标签: wpf validation datagrid tooltip


    【解决方案1】:

    我认为问题在于绑定到给定元素属性 (PlacementTarget) 的相对源,而不是元素本身。但是RelativeSource 标记扩展描述了绑定源相对于给定元素的位置。所以,我所做的是 将 PlacementTarget 设置为原始工具提示目标的祖先:

    <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>
    

    现在它可以按我的意愿工作了。

    【讨论】:

      【解决方案2】:

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

                <TextBlock Text="{Binding RelativeSource={
                                RelativeSource FindAncestor,
                                AncestorType={x:Type DataGridRow},AncestorLevel=1},
                                Path=Item.Error}"
                 TextWrapping="Wrap"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-29
        • 2012-05-28
        • 1970-01-01
        • 2013-07-18
        • 2014-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多