如何使用ToolTip for DataGrid?

时间:2015-09-07 08:34:36

标签: wpf xaml

我试图创建一个弹出窗口,当用户在单元格中重叠鼠标时出现该弹出窗口。我已经看过一些在线教程,并创建了一个工具提示:

<DataGrid>
   <DataGrid.CellStyle>
       <Style>
         <Setter Property="DataGridCell.ToolTip">
               <Setter.Value>bb</Setter.Value>
         </Setter>
       </Style>
    </DataGrid.CellStyle>
    <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding Path = 'position'}" ClipboardContentBinding="{x:Null}" Header="Pos." Width="*"/>
      <DataGridTextColumn Binding="{Binding Path = 'teamName'}" ClipboardContentBinding="{x:Null}" Header="Squadra" Width="*" />
      <DataGridTextColumn Binding="{Binding Path = 'points'}" ClipboardContentBinding="{x:Null}" Header="Punti"  Width="*"/>
      <DataGridTextColumn Binding="{Binding Path = 'playedGames'}" ClipboardContentBinding="{x:Null}" Header="Giocate"  Width="*"/>
      <DataGridTextColumn Binding="{Binding Path = 'goals'}" ClipboardContentBinding="{x:Null}" Header="Goal fatti" Width="*"/>
      <DataGridTextColumn Binding="{Binding Path = 'goalsAgainst'}" ClipboardContentBinding="{x:Null}" Header="Goal subiti" Width="*"/>
      <DataGridTextColumn Binding="{Binding Path = 'goalDifference'}" ClipboardContentBinding="{x:Null}" Header="Differenza reti" Width="*"/>
     </DataGrid.Columns>
</DataGrid>

唯一的问题是当用户与鼠标重叠时,我不明白如何显示单个单元格的全文。特别是当DataGrid最小分辨率时,标题单元格没有完全显示,这是一个问题,为了避免这种情况,我会确保如果用户将鼠标重叠在&#34; pos&#34;上,那么它显示完整标题,因为工具提示是&#34;位置&#34;。我怎么能这样做?

1 个答案:

答案 0 :(得分:5)

对于DataGrid单元格:

如果您希望工具提示在鼠标悬停时显示特定DataGrid单元格的完整内容,则可以修改DataGridTextColumn并对工具提示使用相同的绑定:

<DataGridTextColumn Binding="{Binding Path = 'position'}" ClipboardContentBinding="{x:Null}" Header="Pos." Width="*">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding Path = 'position'}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

或者,如果要为所有列设置一次,则可以绑定到DataGridTextColumn内容的Text属性:

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}" />
    </Style>
</DataGrid.CellStyle>

对于DataGrid列标题:

<DataGrid.ColumnHeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="ToolTip" Value="{Binding}" />
    </Style>
</DataGrid.ColumnHeaderStyle>