How to specify a ToolTip for a control in a Style from XAML? How to specify a ToolTip for a control in a Style from XAML? wpf wpf

How to specify a ToolTip for a control in a Style from XAML?


Figured it out... took me about 6 hours...

For some reason, I can't set the value directly using Value.Setter. If I define the content for the tooltip as a static resource though, and then set it in the Style property of the DataGrid.RowStyle it works.

So, the datagrid row style looks like:

            <Style TargetType="{x:Type dg:DataGridRow}">                <Setter Property="ToolTip" Value="{StaticResource resKWIC}">                </Setter>                             </Style>        </dg:DataGrid.RowStyle>

And the resource is

<Window.Resources>    <StackPanel x:Key="resKWIC">        <TextBlock>f1</TextBlock>        <TextBlock>f2></TextBlock>    </StackPanel></Window.Resources>

Thanks!


The key is to use the Property ToolTipService.ToolTip, instead of ToolTip - like this:

<Setter Property="ToolTipService.ToolTip" Value="My Tooltip"/>


I also got this working with a couple of changes; included incase it helps someone.

My Datadrid is bound to a list of custom objects, I wanted to display the string "Name" as a column and the string "text" in the tooltip. The trick for me (newbe) was that I had to include the Text Column and hide it for it to show up in the tooltip, i.e.:

<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" EnableRowVirtualization="False" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" Name="dgrTextGroupText" VerticalContentAlignment="Stretch" Grid.Column="3" Grid.Row="1" Grid.RowSpan="6" CanUserReorderColumns="False" CanUserSortColumns="False">    <DataGrid.Columns>        <DataGridTextColumn Binding="{Binding Name}" Width="*" />        <DataGridTextColumn Binding="{Binding Text}" Width="0" Visibility="Hidden" />    </DataGrid.Columns>    <DataGrid.RowStyle>        <Style TargetType="{x:Type DataGridRow}">            <Style.Triggers>                <Trigger Property="IsMouseOver" Value="True">                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.text}" />                </Trigger>            </Style.Triggers>        </Style>    </DataGrid.RowStyle></DataGrid>