Wpf – How to specify a ToolTip for a control in a Style from XAML

datagridtooltipwpf

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>

Best Answer

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!

Related Topic