This is a migrated thread and some comments may be shown as answers.

Prevent disabled child checkboxes from being checked through parent

2 Answers 90 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Andy H
Top achievements
Rank 1
Andy H asked on 20 Jul 2011, 01:51 PM
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 Answers, 1 is accepted

Sort by
0
Alex Fidanov
Telerik team
answered on 25 Jul 2011, 12:16 PM
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!

0
Andy H
Top achievements
Rank 1
answered on 04 Aug 2011, 11:11 AM
Thanks Alex. Works like a charm!
Tags
TreeView
Asked by
Andy H
Top achievements
Rank 1
Answers by
Alex Fidanov
Telerik team
Andy H
Top achievements
Rank 1
Share this question
or