Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using a the WPF datagrid from the Microsoft CodePlex project. I have a custom control that I want to databind to a field from the row of the datagrid. I can't for the life of me figure out how to specify a tooltip on a datagrid row.

The closest I've come is to use a RowStyle with a Setter to set the tooltip, but this only seems to work for text. When I try to put a ControlTempalte in as the Value for the ToolTip, it displays the result of calling ToString on the ControlTemplate type.

I think I need to set the "Template" property of the ToolTip, but I can't seem to figure out how to do that...

  <dg:DataGrid Name="dgResults" AutoGenerateColumns="True">

            <dg:DataGrid.RowStyle >


            <Style TargetType="{x:Type dg:DataGridRow}">

                <Setter Property="ToolTip"  >
                    <Setter.Value>

                        <ControlTemplate TargetType="{x:Type ToolTip}">
                           <StackPanel>
                                 <TextBlock>txt1</TextBlock><TextBlock>txt2</TextBlock>
                           </StackPanel>
                        </ControlTemplate>


                    </Setter.Value>
                </Setter>
            </Style>

        </dg:DataGrid.RowStyle>

  </dg:DataGrid>
like image 465
Sean Turner Avatar asked Apr 28 '09 04:04

Sean Turner


People also ask

How do I add ToolTip to XAML?

Use the Placement property or ToolTipService. Placement attached property to place the ToolTip above, below, left, or right of the pointer. You can set the VerticalOffset and HorizontalOffset properties to change the distance between the pointer and the ToolTip.

How to add ToolTip in WPF?

Basic WPF ToolTip ExampleDrag a Button control from the toolbox and drop it. Add a ToolTip property to the button with a message you want to show on mouse hover of the button.


3 Answers

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!

like image 50
Sean Turner Avatar answered Oct 28 '22 23:10

Sean Turner


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

<Setter Property="ToolTipService.ToolTip" Value="My Tooltip"/>
like image 34
TI82 Avatar answered Oct 28 '22 21:10

TI82


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>
like image 1
ValiantBoy Avatar answered Oct 28 '22 23:10

ValiantBoy