【问题标题】:Setting tooltip to equal content将工具提示设置为相同的内容
【发布时间】:2010-07-29 18:41:33
【问题描述】:

我正在尝试将数据网格单元格的工具提示设置为等于该单元格中 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>

但是,它所做的是非常简短地显示工具提示,然后删除单元格中的内容,因此根本没有显示任何内容。此外,从模板设置器外部设置工具提示是一个选项,但我不确定当前绑定是什么。

【问题讨论】:

  • 试试这个:
  • 这会导致它在您将鼠标悬停在单元格上时崩溃。它抛出一个 InvalidOperationException:“指定元素已经是另一个元素的逻辑子元素。先断开它。”

标签: wpf xaml binding tooltip


【解决方案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 或此链接,获取来自 MS http://msdn.microsoft.com/en-us/library/ms752039.aspx 的一堆绑定“操作方法”

【讨论】:

    【解决方案2】:

    您是否尝试过使用RelativeSource?我听说了一些关于 TemplateBinding 与 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

    【讨论】:

    • 第一种方法只是创建空的工具提示。我尝试将祖先设置为 TextBlock 和 DataGridCell。另外我把路径放到文本和内容。第二种方法会导致单元格再次消失,并且工具提示会短暂闪烁。
    【解决方案3】:

    尝试从 ControlTemplate 中移除 ToolTip 并在 Tooltip 的 Style 中定义一个单独的 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>
    

    【讨论】:

      猜你喜欢
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多