我有一个带按钮的userControl
<Button Content="Button" x:Name="button"/>
代码隐藏中的
public Button button { get; set; }
我在另一个页面中使用此userControl
CrudPage
<UC:MyUC x:Name="objectForm" />
可以从CrudPage的代码隐藏中修改此按钮的可见性吗?
答案 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"/>
假设UserControl
有x:Name="userControl"
<UserControl ... x:Name="userControl">
您应该可以从外部控制Visibility
的{{1}},如下所示:
Button