Search code examples
c#wpfxamldatagriddatagridtextcolumn

add a ToolTip on a DataGridTextColumn


I have a DataGrid with some DataGridTextColumn like this:

<DataGrid x:Name="DonneesBrutes" IsReadOnly="True" ItemsSource="{Binding Path=ResultatCollectionGrande}" Margin="10,65,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="PrisEnCompte" Binding="{Binding Path=Flag}" Header="Pris En Compte"></DataGridTextColumn>
        <DataGridTextColumn x:Name="PMRQ" Width="*" Binding="{Binding Path=Pmid}" Header="PMID"></DataGridTextColumn>
        <DataGridTextColumn x:Name="Ligne" Width="40" Binding="{Binding Path=Ligne}" Header="Ligne" IsReadOnly="True"></DataGridTextColumn>
        <DataGridTextColumn x:Name="LibellePMRQ" Width="*" Binding="{Binding Path=LibellePmrq}" Header="Libellé PMRQ">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="ToolTip"
        Value="{Binding RelativeSource={RelativeSource Self},
                        Path=Column.(ToolTipService.ToolTip)}"/>
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
        <DataGridTextColumn x:Name="OTM" Width="*" Binding="{Binding Path=Otm}" Header="OTM"></DataGridTextColumn>
        <DataGridTextColumn x:Name="TOTM" Width="50" Binding="{Binding Path=Totm}" Header="TOTM"></DataGridTextColumn>
        <DataGridTextColumn x:Name="LibelleTOTM" Width="*" Binding="{Binding Path=LibelleTotm}" Header="Libellé TOTM">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="ToolTip"
        Value="{Binding RelativeSource={RelativeSource Self},
                        Path=Column.(ToolTipService.ToolTip)}"/>
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
        <DataGridTextColumn x:Name="GA" Width="70" Binding="{Binding Path=GroupeAlerte}" Header="GA"></DataGridTextColumn>
        <DataGridTextColumn x:Name="Discipline" Width="120" Binding="{Binding Path=Discipline}" Header="Discipline"></DataGridTextColumn>
        <DataGridTextColumn x:Name="DisciplineSubstituee" Width="120" Binding="{Binding Path=DisciplineSubstituee}" Header="Discipline Substituée"></DataGridTextColumn>
        <DataGridTextColumn x:Name="Remarque" Width="*" Binding="{Binding Path=.Remarque}" Header="Remarque"></DataGridTextColumn>

    </DataGrid.Columns>
</DataGrid>

I want to create a popup when the mouse over my DataGridTextColumn LibelleTOTM. To perform this, I have done:

<DataGridTextColumn x:Name="LibelleTOTM" Width="*" Binding="{Binding Path=LibelleTotm}" Header="Libellé TOTM">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={RelativeSource Self},Path=Column.(ToolTipService.ToolTip)}"/>
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>

I have found this solution on internet but it doesn't works on my solution. I have adapted this to my case, but probably not enough... when the mouse is over the LibelleTOTM, nothing happens. Is the TargetType wrong, can't I bind itself like this? I want to open a popup with the value of the DataGridTextColumn, so sending in ToolTip the Binding="{Binding Path=LibelleTotm}" we find on the <DataGridTextColumn x:Name="LibelleTOTM" Width="*" Binding="{Binding Path=LibelleTotm}" Header="Libellé TOTM">

If anyone has an idea about what missing there, what's wrong and what should I do to perform this?

Thanks in advance.

Flo.


Solution

  • the HeaderStyle is applied to the column header. unless i'm missing something in your question, you need to target the DataGridCell itself, like this.

    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
              <Setter Property="ToolTip" Value="{Binding Del_Date, StringFormat=MM/dd/yyyy HH:mm}" />
        </Style>
    </DataGridTextColumn.CellStyle>