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”
    xmlns=”http://schemas.microsoft.com/client/2007
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
             xmlns:data=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”
             Width=”600″ Height=”600″>
    <UserControl.Resources>
        <DataTemplate x:Key=”dt”>
            <StackPanel Orientation=”Horizontal”>
            <TextBlock Text=”This is a tooltip for the Row with content : “></TextBlock>
                <TextBlock Text=”{Binding}”></TextBlock>
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>
    <Grid x:Name=”LayoutRoot” Background=”White”>
        <data:DataGrid x:Name=”dataGrid1″
                       Height=”400″
                       Width=”450″
                       Margin=”0,5,0,10″
                       AutoGenerateColumns=”True” />
        <Popup x:Name=”myPopup”>
            <ContentControl x:Name=”PopupContent” ContentTemplate=”{StaticResource dt}”></ContentControl>
        </Popup>
    </Grid>
</UserControl>

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()
        {
            InitializeComponent();
            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;
        }
      
    }

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s