DataGridRow and Tooltip

We can use Popup to display a tooltip at the row level in the datagrid

Here is sample code

<UserControl x:Class=”slapp1a.Page”
             Width=”600″ Height=”600″>
        <DataTemplate x:Key=”dt”>
            <StackPanel Orientation=”Horizontal”>
            <TextBlock Text=”This is a tooltip for the Row with content : “></TextBlock>
                <TextBlock Text=”{Binding}”></TextBlock>
    <Grid x:Name=”LayoutRoot” Background=”White”>
        <data:DataGrid x:Name=”dataGrid1″
                       AutoGenerateColumns=”True” />
        <Popup x:Name=”myPopup”>
            <ContentControl x:Name=”PopupContent” ContentTemplate=”{StaticResource dt}”></ContentControl>

and in codebehind. all we are doing is handling the PreparingRow event to attach evenhandlers for MouseEvents, where we position the popup and set the content for the contentcontrol in the popup.

Did not try it, but we could change it use storyboards to introduce some delay before showing the tooltip and ofcourse customize the appearance

public partial class Page : UserControl
        public Page()
            dataGrid1.ItemsSource = new string[] { “data 1”, “data 2” };
            dataGrid1.PreparingRow += new EventHandler<DataGridRowEventArgs>(dataGrid1_PreparingRow);           
        }        void dataGrid1_PreparingRow(object sender, DataGridRowEventArgs e)
            e.Row.MouseEnter += new MouseEventHandler(Row_MouseEnter);
            e.Row.MouseLeave += new MouseEventHandler(Row_MouseLeave);

        void Row_MouseLeave(object sender, MouseEventArgs e)
            myPopup.IsOpen = false;

        void Row_MouseEnter(object sender, MouseEventArgs e)
            PopupContent.Content = (sender as DataGridRow).DataContext;
            Point p = e.GetPosition(null);
            myPopup.HorizontalOffset = p.X;
            myPopup.VerticalOffset = p.Y;
            myPopup.IsOpen = true;



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s