简体   繁体   中英

How do you expose a property (DependencyProperty) of a control that is part of a user control

I have added several customised buttons to a user control that I'm creating. On some of these buttons I would like to expose their visibility property to the end user allowing them to decide whether or not they wish them to be visible. How does one go about doing that?

Thanks

If you are deriving your custom/user control from a button then Visibility property should be available in the xaml directly without any changes. But if you want to create a dependency Property then you can follow this approach

public bool ShowHide
        {
            get { return (bool)GetValue(ShowHideProperty); }
            set { SetValue(ShowHideProperty, value); }
        }



public static readonly DependencyProperty ShowHideProperty = DependencyProperty.Register("ShowHide", typeof(bool), typeof(MyControl), new PropertyMetadata(true,OnShowHideChanged));

        private static void OnShowHideChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MyControl c = d as MyControl;
            if(c!=null)
            {
                if((bool)e.NewValue == true)
                {
                    c.Visibility = Visibility.Visible
                }
                else
                {
                    c.Visibility = Visibility.Collapsed;
                }
            }
        }

and in your xaml you can do

<controls:MyControl ShowHide="true" ..../>

EDIT VB Convertion

Public Shared ReadOnly ShowHideProperty As DependencyProperty = DependencyProperty.Register("ShowHide", GetType(Boolean), GetType(MyClass), New FrameworkPropertyMetadata(False, FrameworkPropertyMetadataOptions.AffectsRender, New PropertyChangedCallback(AddressOf onShowHideChanged)))


    Public Property ShowHide() As Boolean
        Get
            Return CBool(GetValue(ShowHideProperty))
        End Get
        Set(ByVal value As Boolean)
            SetValue(ShowHideProperty, value)
        End Set
    End Property

Below is the full vb code

Public ReadOnly ShowHideFirstButtonProperty As DependencyProperty = DependencyProperty.Register("ShowHideFirstButton", GetType(Boolean), GetType(DataNavigator), New FrameworkPropertyMetadata(True, FrameworkPropertyMetadataOptions.AffectsRender, New PropertyChangedCallback(AddressOf onShowHideFirstButtonChanged)))


Public Property ShowHideFirstButton As Boolean
    Get
        Return CBool(GetValue(ShowHideFirstButtonProperty))
    End Get
    Set(ByVal value As Boolean)
        SetValue(ShowHideFirstButtonProperty, value)
    End Set
End Property

Private Sub OnShowHideFirstButtonChanged()
    If ShowHideFirstButton Then
        First.Visibility = Windows.Visibility.Visible 'First being the button whose visibility is to be changed
    Else
        First.Visibility = Windows.Visibility.Collapsed
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM