Tuesday, May 3, 2011

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>
From stackoverflow
  • There's no need for the ControlTemplate. If you want the StackPanel in the ToolTip, just set it as:

    <Setter Property="ToolTip">
        <Setter.Value>
         <StackPanel>
          <TextBlock>txt1</TextBlock><TextBlock>txt2</TextBlock>
         </StackPanel>
        </Setter.Value>
    </Setter>
    

    HTH, Kent

    Sean Turner : You would think so, but this doesn't work. Gives exception: Cannot add content of type 'System.Windows.Controls.StackPanel' to an object of type 'System.Object'. Error at object 'System.Windows.Controls.StackPanel' in markup file 'NewsCluesWpf;component/processdictionary.xaml' Line 31 Position 31.
  • Not sure you can do it through XAML.

    A easier way might be to just handle the LoadingRow event. In xaml have something like:

    <dg:DataGrid Name="dgResults" AutoGenerateColumns="True" 
                 LoadingRow="dgResults_LoadingRow" 
                 ItemsSource="{Binding ListOfStrings}" />
    

    Then in code behind

    void dgResults_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        DataGridRow row = e.Row;
        row.ToolTip = row.DataContext as string;
    }
    

    Obviously you will have to change the code depending on how you are populating the data in the datagrid. This is also untested =)

  • 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!

  • You can use a IMultiValueConverter like the example below:

    http://codefornothing.wordpress.com/2009/01/25/the-wpf-datagrid-and-me/

    I based my implementation on that example and substituted the BackgroundProperty with the ToolTipProperty.

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

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

0 comments:

Post a Comment