更改userControl中的可见性

时间:2014-03-29 16:48:09

标签: c# user-controls winrt-xaml

我有一个带按钮的userControl

<Button Content="Button" x:Name="button"/>
代码隐藏中的

public Button button { get; set; }

我在另一个页面中使用此userControl

CrudPage

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

可以从CrudPage的代码隐藏中修改此按钮的可见性吗?

1 个答案:

答案 0 :(得分:1)

DenedencyProperty中创建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); }
}

将其绑定到Button.Visibility

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

假设UserControlx:Name="userControl"

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

您应该可以从外部控制Visibility的{​​{1}},如下所示:

Button