Binding by relative source is not working in Tooltip WPF

Hari Prasad M 1 Reputation point
2020-10-09T07:50:22.983+00:00

I am trying to bind the tooltip foreground in the textblock present inside the stackpanel for displaying the tooltip text. But the binding doesnt work properly in the textblock ie.. tooltip foreground is not applied.Here is the snippet which is provided below.

MainWindow.xaml

<Grid>
<CheckBox Grid.Row="5" Grid.Column="1" HorizontalAlignment="Center" Margin="0,4,10,0" VerticalAlignment="Center" IsChecked="{Binding AutoAdd, Mode=TwoWay}">
<CheckBox.ToolTip>
<StackPanel>
<TextBlock Foreground="{Binding Path=Foreground, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}" FontWeight="Bold" FontSize="14" Margin="0,0,0,5">Automatically Add To Path</TextBlock>
<TextBlock Foreground="{Binding Path=Foreground, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}">
If this path is associated with the primary configuration,
<LineBreak />
automatically add newly instantiated optical elements to the end of the path.
</TextBlock>
<Border BorderBrush="Silver" BorderThickness="0,1,0,0" Margin="0,8" />
<WrapPanel>
<Image Margin="0,0,5,0" />
<TextBlock Foreground="{Binding Path=Foreground, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}" FontStyle="Italic">Press F1 for more help</TextBlock>
</WrapPanel>
</StackPanel>
</CheckBox.ToolTip>
</CheckBox>
</Grid>

So please provide me a solution for this issue.

Regards,

Hari Prasad

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,691 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 113.7K Reputation points
    2020-10-09T17:30:04.967+00:00

    Try adding the <ToolTip> element:

     . . .
    <CheckBox.ToolTip>
       <ToolTip>
          <StackPanel>
             . . .
          </StackPanel>
       </ToolTip>
    </CheckBox.ToolTip>
    . . .
    
    0 comments No comments

  2. DaisyTian-1203 11,616 Reputation points
    2020-10-12T03:04:41.873+00:00

    In the xaml , there are two ways to set ToolTip for CheckBox. One is to define a tooltip by assigning container/control to the ToolTip property, like what you did. And the other is to define a tooltip by assigning a ToolTip object to the ToolTip property, like what Viorel-1 did. You can refer to here for more details. You can set the ToolTip's Foreground in the ToolTip object like below shown:

     <CheckBox Grid.Row="5" Grid.Column="1" HorizontalAlignment="Center" Margin="0,4,10,0" VerticalAlignment="Center" IsChecked="{Binding AutoAdd, Mode=TwoWay}">  
                <CheckBox.ToolTip>  
                    <ToolTip Foreground="BlueViolet">  
                        <StackPanel>  
                            <TextBlock Foreground="{Binding Path=Foreground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"   
                                   FontWeight="Bold" FontSize="14" Margin="0,0,0,5">Automatically Add To Path</TextBlock>  
                            <TextBlock Foreground="{Binding Path=Foreground, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}">  
                            If this path is associated with the primary configuration,  
                            <LineBreak />  
                            automatically add newly instantiated optical elements to the end of the path.  
                            </TextBlock>  
                            <Border BorderBrush="Silver" BorderThickness="0,1,0,0" Margin="0,8" />  
                            <WrapPanel>  
                                <Image Margin="0,0,5,0" />  
                                <TextBlock Foreground="{Binding Path=Foreground, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"   
                                       FontStyle="Italic">Press F1 for more help</TextBlock>  
                            </WrapPanel>  
                        </StackPanel>  
                    </ToolTip>  
                </CheckBox.ToolTip>  
            </CheckBox>  
    

    If I misunderstand your question,please point out.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments