Search code examples
c#wpftooltip

Style not taken in account on GridViewColumn ToolTip


So I have the following problem :

In my ListView, I want to add ToolTips to specific GridViewColumns. Sometimes these ToolTips are empty, and I need to hide them.

When I have a ToolTip on a ListView Line, I encounter no problem doing the following in my App.xaml file:

<Style TargetType="ToolTip">
   <Style.Triggers>
      <Trigger Property="Content" Value="{x:Static sys:String.Empty}">
         <Setter Property="Visibility" Value="Collapsed" />
      </Trigger>
      <Trigger Property="Content" Value="{x:Null}">
         <Setter Property="Visibility" Value="Collapsed" />
      </Trigger>
   </Style.Triggers>
</Style>

But in the case a ToolTip is applied to only one column of my ListView, my XAML is the following :

<GridViewColumn Header="{x:Static p:Resources.Name}" Width="100">
   <GridViewColumn.CellTemplate>
      <DataTemplate>
         <Grid>
            <TextBlock Text="{Binding Path=Name}" Tag="{Binding Name}" 
                       MouseMove="mouseOverNameRepere">
               <TextBlock.ToolTip>
                   <StackPanel>
                      <Grid>
                         <TextBlock Grid.Column="0" 
                                    Text="{Binding Path=ToolTipModifications}" 
                                    TextAlignment="Left" HorizontalAlignment="Left"/>
                      </Grid>
                   </StackPanel>
               </TextBlock.ToolTip>
            </TextBlock>
         </Grid>
      </DataTemplate>
   </GridViewColumn.CellTemplate>
</GridViewColumn>

How can I hide the ToolTip when it is empty? The code in my App.xaml is not working. Also tried to do it in code behind :

TextBlock item = (TextBlock)sender;
ToolTip toolTip = (ToolTip)item.ToolTip;

But second line gives me an exception as item.ToolTip is a StackPanel object and cannot be converted? In fact I calculate the ToolTip content only when I enter the TextBox element, so I thought I would then check and apply toolTip.Visibility at this moment, but I couldn't.


Solution

  • Your Style should work if you set the ToolTip property of the TextBlock like this:

    <TextBlock Text="{Binding Path=Name}" Tag="{Binding Name}" MouseMove="mouseOverNameRepere" 
               ToolTip="{Binding Path=ToolTipModifications}" />