Xceed Community DataGridControl Watermark Removal

Update 2
I put together a sample application to show this code working.

I’m all for accreditation of code. Especially when it is released open source. However, when your open source code contains an “unremovable” watermark it seems to me that has crossed a line. Why not just release a closed-source lite version of the software and retain rights to it.

For the record I could easily download your source and remove the offending watermark code. I decided to do the community a favor and post an extension for the Xceed Grid from the Extended WPF Toolkit to remove the watermark in C#.

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using Xceed.Wpf.DataGrid;
namespace Common.Extensions
{
    public static class XceedDataGridExtensions
    {
        public static void RemoveWaterMark(this DataGridControl grid)
        {
            object hgbc = XceedDataGridExtensions.FindChild(grid, null);
            if (hgbc != null)
            {
                AdornerLayer al = AdornerLayer.GetAdornerLayer(hgbc as Control);
                al.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                AdornerLayer gridAdorner = AdornerLayer.GetAdornerLayer(grid);

                if (gridAdorner != null)
                    gridAdorner.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        static T FindChild(DependencyObject parent, string childName) where T : DependencyObject
        {
            // Confirm parent and childName are valid.
            if (parent == null) return null;
            T foundChild = null;
            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                // If the child is not of the request child type child
                T childType = child as T;
                if (childType == null)
                {
                    // recursively drill down the tree
                    foundChild = FindChild(child, childName);
                    // If the child is found, break so we do not overwrite the found child.
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    // If the child's name is set for search
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        // if the child's name is of the request name
                        foundChild = (T)child;
                        break;
                    }
                }
                else
                {
                    // child element found.
                    foundChild = (T)child;
                    break;
                }
            }
            return foundChild;
        }
    }
}

I’d like to state that I’m not advocating not attributing credit where credit is do. I already have an about page that states all the controls I use, who maintains them, and of course if I alter code I make sure to submit it back. Please make sure that if you do use this code that you are providing proper credit to the hard working developers that make you developing that much easier.

Thanks!

Update
Thanks to Jeremy’s comment I added code to remove the watermark when the “Group by Header” is hidden. Thank you Jeremy!

Update #2
I put together a sample application to show this code working.

About Mike

I'm a software engineer. Look into the about page for more information about me.
Tagged , , . Bookmark the permalink.

5 Responses to Xceed Community DataGridControl Watermark Removal

  1. Jeremy says:

    Mike, I also recommend adding the following snippet to your extension method, to also remove the watermark from the bottom-left corner when the Group By header is disabled.

    AdornerLayer adorner = AdornerLayer.GetAdornerLayer(grid);

    if (adorner != null)
    adorner.Visibility = Visibility.Collapsed;

    • Mike says:

      Sweet! Thanks for the tip. Code is updated. I’ll double check it when I get a chance to merge it into my support library tomorrow morning.

  2. Sam Bendall says:

    Thanks, this works very well.
    Useful to Hide the group by header too by setting hgbc.Visibility to Collapsed.

    • Mike says:

      That’s the first way I was hiding the watermark. After playing around with the grouping function I liked it so much I really wanted to keep it but loose the watermark.

      I have plans on pulling source on the project and changing a couple of layout things that I don’t like. The weird ‘X’ when pulling the grouped header off has got to be changed.

  3. Pingback: Xceed DataGridControl Watermark Removal Example Application - It's Not Found