Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
845
How can I set the tooltip on a xamGrid TextColumn from C#?
posted

I have a xamGrid and need to set the header text and tooltip from the code behind.

If I don't want the tooltip I can do the following:

                        <ig:TextColumn Key="SubDescription3"
                                       HeaderText="{Binding pItemsList.SubDescription3, Source={StaticResource pItemsList}}"
                                       Width="*"
                                       IsGroupable="False"/>

Then when the grid is loaded I have:

        private void gvItemMasterList_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (Column col in this.gvItemMasterList.Columns.DataColumns)
            {
                switch (col.Key)
                {
                    case "SubDescription1":
                    case "SubDescription2":
                    case "SubDescription3":
                        {
                            col.HeaderText = CustomCaptionService.GetCustomCaption("ItemMaster", col.Key);
                        }
                        break;
                }
            }
        }

Which pulls the header text out of the database so that it can be configured at runtime.

However, if I want to set up a tooltip I would normally do the following in XAML:

                        <ig:TextColumn Key="SubDescription1"
                                       Width="0.8*"
                                       Visibility="Collapsed">
                            <ig:TextColumn.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock ToolTipService.ToolTip="{Binding pItemsList.SubDescription1, Source={StaticResource pItemsList}}" Text="{Binding pItemsList.SubDescription1, Source={StaticResource pItemsList}}"/>
                                <DataTemplate>
                             <ig:TextColumn.HeaderTemplate>
                         <ig:TextColumn> 

However, if I do this I can't set the HeaderText property in the code behind.

So, what do I need to do in the code behind so that I can have a tooltip and have both the header text and tooltip text set to the value calculated at runtime?

  • 845
    Verified Answer
    Offline posted

    You have to create the DataTemplate from XAML which can be built up in code:

            private void gvItemMasterList_Loaded(object sender, RoutedEventArgs e)
            {
                foreach (Column col in this.gvItemMasterList.Columns.DataColumns)
                {
                    switch (col.Key)
                    {
                        case "SubDescription1":
                        case "SubDescription2":
                        case "SubDescription3":
                            {
                                string headerText = CustomCaptionService.GetCustomCaption("ItemMaster", col.Key);
                                col.HeaderTemplate = (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">
                                        <TextBlock ToolTipService.ToolTip=""" + headerText + @"""
                                                   Text=""" + headerText + @"""/>
                                    </DataTemplate>");
                            }
                            break;
                    }
                }
            }