无法设置DataGridColumn的ToolTip

时间:2009-11-14 20:58:02

标签: wpf datagrid wpf-controls wpftoolkit datagridcolumn

我尝试了以下内容:

<tk:DataGridTextColumn 
    Header="Item" 
    Binding="{Binding Item.Title}" 
    ToolTipService.ToolTip="{Binding Item.Description}" />

我没有看到任何工具提示。

有什么想法吗? 它甚至实现了吗?

5 个答案:

答案 0 :(得分:27)

这对我有用:

<Style TargetType="{x:Type Custom:DataGridColumnHeader}">
   <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="ToolTip" Value="{Binding Column.(ToolTipService.ToolTip), RelativeSource={RelativeSource Self}}"/>
      </Trigger>
   </Style.Triggers>
</Style>

答案 1 :(得分:8)

请检查下面的代码是否适合你,它应该显示列标题和单元格的工具提示,单元格的工具提示应该弯曲数据对象的描述字段:

<DataGridTextColumn Width="SizeToCells"   
                    MinWidth="150" 
                    Binding="{Binding Name}">

    <DataGridTextColumn.Header>
        <TextBlock Text="Name" ToolTipService.ToolTip="Header ToolTip" />
    </DataGridTextColumn.Header>

    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="ToolTip" Value="{Binding Description}" />
            <Setter Property="TextWrapping" Value="Wrap" />
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

此处找到解决方案:5 Random Gotchas with the WPF DataGrid

答案 2 :(得分:5)

DataGridTextColumn不可见。您必须在标题或内容上设置工具提示。

要在标题上设置工具提示,请将标题更改为TextBlock:

<tk:DataGridTextColumn
  Binding="{Binding Item.Title}">
  <tk:DataGridTextColumn.Header>
    <TextBlock
      Text="Text" 
      ToolTipService.ToolTip="Tooltip for header" />
  </tk:DataGridTextColumn.Header>
</tk:DataGridTextColumn>

要在列内容上设置工具提示,请在样式中设置它:

<tk:DataGridTextColumn
  Binding="{Binding Item.Title}"
  Heading="Text">
  <tk:DataGridTextColumn.ElementStyle>
    <Style>
      <Setter Property="ToolTipService.ToolTip" Value="{Binding Item.Description}" />
    </Style>
  </tk:DataGridTextColumn.ElementStyle>
</tk:DataGridTextColumn>

您可能还想设置EditingElementStyle

答案 3 :(得分:1)

此外,如果您的列是DataGridTemplateColumn而不是DataGridTextColumn,您可以这样做:

<DataGridTemplateColumn x:Name="MyCheckBoxColumn" CellStyle="{StaticResource MyCellStyle}" >
    <DataGridTemplateColumn.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="MyHeaderName" ToolTip="This is my column description" />
        </DataTemplate>
    </DataGridTemplateColumn.HeaderTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox ... />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

答案 4 :(得分:0)

以标题样式设置ToolTipService.ToolTip属性:

<Setter Property="ToolTipService.ToolTip" Value="{x:Static res:StringResources.List_Dialog_SelectAll_Checkbox}"/>

这是我在DataGridCheckBoxColumn中使用图像而不是文本时使用它的方式。 XAML:

    <Window x:Class="MyProject.GUI.ListDialog"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:viewModel="clr-MyProject.GUI.ViewModels" 
            Title="{Binding Title}"  Height="350" Width="650"
            MinHeight="350" MinWidth="650"
            xmlns:res="clr-MyProject.GUI.Resources" Closing="Window_Closing" WindowStyle="ToolWindow">
    <Window.Resources>
            <BitmapImage x:Key="MyImageSource" UriSource="Resources/Images/SelectDeselect.png" />
           <Style x:Key="CheckBoxHeader"  TargetType="DataGridColumnHeader">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                            <Setter Property="ToolTipService.ToolTip" Value="{x:Static res:StringResources.List_Dialog_SelectAll_Checkbox}"/>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Image Width="15" Height="15" Source="{StaticResource MyImageSource}" />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    </Window.Resources>

C#:

DataGridCheckBoxColumn checkColumn = new DataGridCheckBoxColumn();
checkColumn.HeaderStyle = new System.Windows.Style();
checkColumn.CanUserSort = checkColumn.CanUserResize = false;
checkColumn.Width = new DataGridLength(25);
checkColumn.HeaderStyle = (Style)Resources["CheckBoxHeader"];
checkColumn.CellStyle = (Style)Resources["CenterAlignedCellStyle"];
checkColumn.IsReadOnly = false;
dataGrid.Columns.Add(checkColumn);