views:

115

answers:

2

I'm trying to set a data grid's cell's tooltip to be equal to the text inside of a TextBlock in that cell. What I have so far is this:

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

However, what this does is very briefly show the tooltip and then the content in the cell is removed, so nothing shows up at all. Also, setting the tooltip from outside the Template setter is an option, but I'm not sure what the corrent binding is to do that.

A: 

Have you tried using RelativeSource? I heard of some issues about TemplateBinding vs. RelativeSource (http://stackoverflow.com/questions/1131222/wpf-templatebinding-vs-relativesource-templatedparent).

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

Where "YourAncestorType" is the type of the parent you want to find.

Or you could also try the same approach with

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

See also: http://www.wpfwiki.com/Default.aspx?Page=WPF%20Q5.3&amp;AspxAutoDetectCookieSupport=1

Torsten
The first method just creates empty tooltips. I tried setting the ancestor to TextBlock and DataGridCell. Also I put the path to Text and Content.The second method causes the cells to disappear again and the tooltips flash breifly.
wangburger
A: 

Try removing the ToolTip from the ControlTemplate and defining a separate Setter in the Style for the Tooltip.

Here is the XAML using your sample:

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