Question

i got an userControl with a button

<Button Content="Button" x:Name="button"/>

in the codebehind

public Button button { get; set; }

I use this userControl in another page

CrudPage

 <UC:MyUC x:Name="objectForm" />

is possible to modify visibily of this button from the codebehind of the CrudPage?

Was it helpful?

Solution

Create DenedencyProperty in UserControl:

public static DependencyProperty ButtonVisibilityProperty = DependencyProperty.Register("ButtonVisibility", typeof(Visibility), typeof(MyUserControl), null);

public Visibility ButtonVisibility
{
    get { return (Visibility)GetValue(ButtonVisibilityProperty); }
    set { SetValue(ButtonVisibilityProperty, value); }
}

bind it to Button.Visibility:

<Button 
    Visibility="{Binding ElementName=userControl, Path=ButtonVisibility}" 
    Content="Button" 
    x:Name="button"/>

assuming that UserControl has a x:Name="userControl"

<UserControl ... x:Name="userControl">

you should be able to control Visibility of a Button from outside like so:

<local:MyUserControl ButtonVisibility="Collapsed"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top