Prevent disabled child checkboxes from being checked through parent

3 posts, 0 answers
  1. Andy H
    Andy H avatar
    12 posts
    Member since:
    Oct 2009

    Posted 20 Jul 2011 Link to this post

    I am using the RadTreeView with CheckList and HierarchicalDataTemplates as described here.

    Some of the child items in the treeview can be disabled and are naturally greyed out. However, when checking one of the parent nodes, the disabled child nodes are also selected.

    We would like to prevent these disabled items from being checked as our users are finding this counterintuitive.

    One could off course prevent this from happening in our object model, but ideally we would like the control to take care of this selection.
     
    Please advise whether this is possible.

    Regards
  2. Alex Fidanov
    Admin
    Alex Fidanov avatar
    636 posts

    Posted 25 Jul 2011 Link to this post

    Hello Andy H,

    What I would suggest is to create an extension class and an attached proeprty that would handle this scenario without populating the rest of the code. For example:
    public class TreeViewExtensions : DependencyObject
        {
            public static bool GetDisallowCheckOnDisabledItems(DependencyObject obj)
            {
                return (bool)obj.GetValue(DisallowCheckOnDisabledItemsProperty);
            }
     
            public static void SetDisallowCheckOnDisabledItems(DependencyObject obj, bool value)
            {
                obj.SetValue(DisallowCheckOnDisabledItemsProperty, value);
            }
     
            public static readonly DependencyProperty DisallowCheckOnDisabledItemsProperty =
                DependencyProperty.RegisterAttached("DisallowCheckOnDisabledItems", typeof(bool), typeof(RadTreeView),
                new PropertyMetadata(false, OnDisallowCheckOnDisabledItemsChanged));
     
            private static void OnDisallowCheckOnDisabledItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
            {
                RadTreeView owner = d as RadTreeView;
                if (owner != null)
                {
                    bool newValue = (bool)args.NewValue;
                    if (newValue)
                    {
                        owner.PreviewChecked += OnPreviewChecked;
                    }
                    else
                    {
                        owner.PreviewChecked -= OnPreviewChecked;
                    }
                }
            }
     
            static void OnPreviewChecked(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {
                RadTreeViewItem item = e.OriginalSource as RadTreeViewItem;
                e.Handled = item != null && !item.IsEnabled;
            }
        }
    Applying the behavior:

    <telerik:RadTreeView extensions:TreeViewExtensions.DisallowCheckOnDisabledItems="True" ...
    Please let me know if you have questions on this matter.

    All the best,
    Alex Fidanov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. DevCraft banner
  4. Andy H
    Andy H avatar
    12 posts
    Member since:
    Oct 2009

    Posted 04 Aug 2011 Link to this post

    Thanks Alex. Works like a charm!
Back to Top