none
Overriding Visibility property

    Question

  • Hi all,

    I'm developing a control library and I would like to add "VisibleUserLevel" property to each control I create. The property is for managing user interface complexity; it is used to hide all controls with "VisibleUserLevel" < "CurrentUserLevel".

    The problem I ran into is I can not override Visibility property. In Windows.Forms I would do something like this:

    1        private bool _visible; 
    2 
    3        protected override void SetVisibleCore(bool value) 
    4        { 
    5            _visible = value; 
    6            base.SetVisibleCore(_visible && (VisibleUserLevel >= CurrentUserLevel)); 
    7        } 
    8 
    9        private void OnUserLevelChanged(object sender, EventArgs e) 
    10        { 
    11            SetVisibleCore(_visible); 
    12        } 

    How should I approach to the problem in WPF?

    Robert


    Robert
    Friday, October 24, 2008 8:52 AM

Answers

  • Because the Visibility property is a dependency property, I think you could hook into it with a CoerceValueCallback. The callback fires even before the property is updated to the new value. So, at that point you can intercept and change the new value just before the property would actually be set.

    It would look like this:

    using System.Windows;  
    using System.Windows.Controls;  
     
    namespace AddOwnerTest  
    {  
        public class MyTextBox : TextBox  
        {  
            static int VisibleUserLevel = 1;    // defined here for demo purposes  
            static int CurrentUserLevel = 2;    // defined here for demo purposes  
     
            static MyTextBox()  
            {  
                UIElement.VisibilityProperty.AddOwner(  
                    typeof(MyTextBox),   
                    new FrameworkPropertyMetadata(nullnew CoerceValueCallback(CoerceVisibilityValue)));  
            }  
     
            protected override void OnInitialized(System.EventArgs e)  
            {  
                base.OnInitialized(e);  
     
                // Check initial value  
                if (this.Visibility == Visibility.Visible)  
                {  
                    if (VisibleUserLevel < CurrentUserLevel)  
                        this.Visibility = Visibility.Collapsed;  
                }  
            }  
     
            /// <summary>  
            /// Coerces the Visibility value.  
            /// </summary>  
            private static object CoerceVisibilityValue(DependencyObject d, object value)  
            {  
                if ((Visibility)value != Visibility.Visible )   
                    return value;  
     
                if (VisibleUserLevel >= CurrentUserLevel)  
                    return Visibility.Visible;  
                else 
                    return Visibility.Collapsed;  
            }  
     
        }  
    }  
     

    hth,
    Marcel

    • Marked as answer by iinlane Monday, October 27, 2008 8:34 AM
    Friday, October 24, 2008 1:31 PM