Wpf 将工具提示设置为相等内容

Wpf 将工具提示设置为相等内容,wpf,xaml,binding,tooltip,Wpf,Xaml,Binding,Tooltip,我试图将数据网格单元格的工具提示设置为与该单元格中文本块内的文本相等。到目前为止,我得到的是: <Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="DataGridCel

我试图将数据网格单元格的工具提示设置为与该单元格中文本块内的文本相等。到目前为止,我得到的是:

<Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridCell">
                    <Grid>                         
                        <TextBlock Margin="2" VerticalAlignment="Center" 
                                HorizontalAlignment="Left"  TextWrapping="Wrap" >
                            <ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" />
                            <TextBlock.ToolTip>
                                <ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" />
                            </TextBlock.ToolTip>
                        </TextBlock>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>            
    </Style>


但是,这样做只是非常简短地显示工具提示,然后删除单元格中的内容,因此根本不会显示任何内容。另外,从模板设置器外部设置工具提示也是一个选项,但我不确定相应的绑定是如何实现的。

您是否尝试过使用RelativeSource?我听说了一些关于TemplateBinding与RelativeSource()的问题


其中“YourAncestorType”是您要查找的父级的类型

或者,你也可以尝试同样的方法

<ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />


另请参见:

尝试从ControlTemplate中删除工具提示,并在工具提示样式中定义单独的Setter

以下是使用您的示例的XAML:

<Style x:Key="CellStyle" TargetType="{x:Type WpfToolkit:DataGridCell}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="WpfToolkit:DataGridCell">
        <Grid>
          <TextBlock Margin="2" VerticalAlignment="Center"  
                     HorizontalAlignment="Left"  TextWrapping="Wrap" > 
            <ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" /> 
            <!--<TextBlock.ToolTip> 
              <ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" /> 
            </TextBlock.ToolTip>-->
          </TextBlock>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
  <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}"/>
</Style>

我这里的示例是一个简单的标签,但这可以应用于其他控件

<Label Name="lblFormName" Content="Form Elements:" FontWeight="Bold" HorizontalAlignment="Left" Width="295" >
                    <Label.ToolTip>
                        <Binding ElementName="lblFormName" Path="Content"/>
                    </Label.ToolTip>
                </Label>

查看此链接:
或者,这是一组来自MS的绑定“how-to”的链接,试试这个:当您将鼠标悬停在单元格上时,它会崩溃。它抛出InvalidOperationException:“指定的元素已经是另一个元素的逻辑子元素。请先断开它。”第一个方法只创建空工具提示。我尝试将祖先设置为TextBlock和DataGridCell。我还把路径放在文本和内容上。第二种方法使单元格再次消失,工具提示快速闪烁。
<Label Name="lblFormName" Content="Form Elements:" FontWeight="Bold" HorizontalAlignment="Left" Width="295" >
                    <Label.ToolTip>
                        <Binding ElementName="lblFormName" Path="Content"/>
                    </Label.ToolTip>
                </Label>