Unable to set DataGridColumn's ToolTip

前端 未结 5 1495
春和景丽
春和景丽 2021-01-01 21:22

I tried the following:



        
相关标签:
5条回答
  • 2021-01-01 22:01

    Set ToolTipService.ToolTip Property in Header style:

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

    Here it is how I used it when I had image in DataGridCheckBoxColumn instead of text. 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);
    
    0 讨论(0)
  • 2021-01-01 22:08

    The DataGridTextColumn is not visible. You have to set tooltips on the header or the content.

    To set a ToolTip on the header, change the Header to a TextBlock:

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

    To set a ToolTip on the column contents, set it in the Style:

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

    You may also want to set EditingElementStyle.

    0 讨论(0)
  • 2021-01-01 22:12

    pls, check if the code below would work for you, it should be displaying tooltips for columns headers and cells, cell's tooltip should be bent the Description field of the data object:

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

    solution found here: 5 Random Gotchas with the WPF DataGrid

    0 讨论(0)
  • 2021-01-01 22:14

    Additionally, if your column is a DataGridTemplateColumn instead of a DataGridTextColumn, you can do it like this:

    <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>
    
    0 讨论(0)
  • 2021-01-01 22:15

    This works for me:

    <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>
    
    0 讨论(0)
提交回复
热议问题