Setting tooltip to equal content

霸气de小男生 提交于 2019-12-24 05:30:09

问题


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.


回答1:


My example here is for a simple label but this can be applied to other controls.

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

Check out this link: http://msdn.microsoft.com/en-us/library/ms742167.aspx or this link for a bunch of binding "how-to"s from MS http://msdn.microsoft.com/en-us/library/ms752039.aspx




回答2:


Have you tried using RelativeSource? I heard of some issues about TemplateBinding vs. RelativeSource (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&AspxAutoDetectCookieSupport=1




回答3:


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>


来源:https://stackoverflow.com/questions/3365909/setting-tooltip-to-equal-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!