2010-07-29 62 views
2

我试图设置数据网格的单元格的工具提示等于该单元格中的TextBlock内部的文本。我到目前为止是这样的:设置工具提示等于内容

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

然而,这样做是很简单的显示工具提示,然后在单元格中的内容被删除,所以什么都没有显示出来。另外,从模板设置器外部设置工具提示是一个选项,但我不确定该绑定是如何实现的。

+0

尝试这样: Carlo 2010-07-29 23:57:57

+1

当您将鼠标悬停在单元格上时,会导致其崩溃。它会抛出一个InvalidOperationException异常:“指定的元素已经是另一个元素的逻辑子元素,请先断开它。” – wangburger 2010-07-30 01:31:09

回答

0

您是否尝试过使用RelativeSource?我听说有关TemplateBinding vs. RelativeSource的一些问题(WPF TemplateBinding vs RelativeSource TemplatedParent)。

<ContentPresenter Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourAncestorType}, AncestorLevel=1},Path=Content}" /> 

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

或者你也可以尝试用

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

也看到了同样的方法:http://www.wpfwiki.com/Default.aspx?Page=WPF%20Q5.3&AspxAutoDetectCookieSupport=1

+0

第一种方法只是创建空的工具提示。我尝试将祖先设置为TextBlock和DataGridCell。此外,我把路径文本和内容。 第二种方法导致单元再次消失,工具提示闪烁不定。 – wangburger 2010-07-30 12:55:17

0

尝试删除从控件模板工具提示和样式的工具提示定义一个单独的二传手。

下面是使用你的样品的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> 
1

我在这里的例子是一个简单的标签,但这可以应用到其他控件。

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

退房此链接:http://msdn.microsoft.com/en-us/library/ms742167.aspx 或此链接的一群结合的 “如何做” S从MS http://msdn.microsoft.com/en-us/library/ms752039.aspx