改变userControl的可见性

本文关键字:可见性 userControl 改变 | 更新日期: 2023-09-27 17:54:44

我得到了一个带有按钮的userControl

<Button Content="Button" x:Name="button"/>
后面代码中的

public Button button { get; set; }

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

CrudPage

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

是否可以修改这个按钮的可见性从CrudPage的代码背后?

改变userControl的可见性

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); }
}

绑定Button.Visibility:

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

假设UserControlx:Name="userControl"

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

你应该能够从外部控制ButtonVisibility,像这样:

<local:MyUserControl ButtonVisibility="Collapsed"/>