8

这是我的网格,当鼠标悬停在该标题上以显示 expl 时,我要对标题“RED.BROJ”进行解释。文本。

<ListView.View>
    <GridView>
        <GridViewColumn Width="50"
                        Header="Реd.Број"
                        DisplayMemberBinding="{Binding Path=RedenBroj}">
        </GridViewColumn>
4

3 回答 3

9

你可以这样做:

<GridViewColumn Width="50"
                DisplayMemberBinding="{Binding Path=RedenBroj}">
    <GridViewColumn.Header>
        <TextBlock Text="Ред.Број"
                   ToolTip="Your explanation" />                      
    </GridViewColumn.Header>        
</GridViewColumn>
于 2012-06-22T12:48:43.790 回答
9

Slightly late response but you can add a tooltip, without losing the ability to drag columns to reorder them, by doing the following:

<GridViewColumn Width="50"
                Header="Реd.Број"
                DisplayMemberBinding="{Binding Path=RedenBroj}">
    <GridViewColumn.HeaderContainerStyle>
        <Style>
            <Setter Property="Control.ToolTip" Value="Tool tip content"/>
        </Style>
    </GridViewColumn.HeaderContainerStyle>
</GridViewColumn>

Update: more concise version thanks to LPL

Further update: I wanted to be able to have all columns have tooltips that match their headers (as some columns were too narrow to show the whole header):

<ListView.View>
    <GridView>
        <GridView.ColumnHeaderContainerStyle>
            <Style TargetType="GridViewColumnHeader">
                <Setter Property="ToolTip"
                        Value="{Binding Content, RelativeSource={RelativeSource Self}}"/>
            </Style>
        </GridView.ColumnHeaderContainerStyle>

        <GridViewColumn DisplayMemberBinding="{Binding A}" Header="A"/>
        <GridViewColumn DisplayMemberBinding="{Binding B}" Header="B"/>
        <GridViewColumn DisplayMemberBinding="{Binding C}" Header="C"/>
    </GridView>
</ListView>
于 2015-04-23T13:16:56.677 回答
0

没有什么比用自己的方式回答一个老问题更好的了……

我受到@Scroog1 的回答的启发,但似乎有点多余,Tooltip它只是模仿那里的内容。您通常需要 ,Tooltip因为您已经缩写了列标题文本。

我创建了一个AttachedPropertyTooltipGridViewColumn. 我再到bind此从我Style为我GridViewColumnHeader

现在我只定义Style一次,然后添加它以及AttachedProperty我想在哪里使用它。

Xaml

    <Style x:Key="GridViewColumnHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="ToolTip" Value="{Binding Path=Column.(attachedProperties:GridViewColumnHeaderToolTipAttachedProperty.Tooltip), RelativeSource={RelativeSource Self}}" />
    </Style>

    <GridView x:Key="GridViewFuelConsumption"
              x:Shared="False">
        <GridViewColumn Header="Ред.Број"
                        DisplayMemberBinding="{Binding RedenBroj}"
                        HeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle}"
                        attachedProperties:GridViewColumnHeaderToolTipAttachedProperty.Tooltip="Your explanation" />
    </GridView>

附加属性

public sealed class GridViewColumnHeaderToolTipAttachedProperty : DependencyObject
{
    public static readonly DependencyProperty TooltipSourceProperty = DependencyProperty.RegisterAttached(
        "Tooltip",
        typeof(string),
        typeof(GridViewColumnHeaderToolTipAttachedProperty),
        new PropertyMetadata("null"));

    public static void SetTooltip(DependencyObject element, string value)
    {
        element.SetValue(TooltipSourceProperty, value);
    }

    public static string GetTooltip(DependencyObject element)
    {
        return (string)element.GetValue(TooltipSourceProperty);
    }
}
于 2021-03-28T21:45:18.670 回答