无法设置 DataGridColumn 的工具提示

发布于 2024-08-11 08:03:39 字数 234 浏览 6 评论 0原文

我尝试了以下操作:

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

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

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

I tried the following:

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

And I don't see any tool tip.

Any ideas?
Is it even implemented?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何处潇湘 2024-08-18 08:03:39

这对我有用:

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

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>
溺ぐ爱和你が 2024-08-18 08:03:39

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

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

在这里找到解决方案:WPF DataGrid 的 5 个随机问题

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

烛影斜 2024-08-18 08:03:39

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

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.

遇到 2024-08-18 08:03:39

此外,如果您的列是 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>

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>
一片旧的回忆 2024-08-18 08:03:39

在标题样式中设置 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文