我有一个具有多个按钮的用户控件,并且在应用程序中我在3个窗口上使用此用户控件,...当应用程序运行但是我要显示时,此3窗口中用户控件的编辑按钮是折叠的当我打开一个特定的窗口时,在所有窗口中编辑按钮,如果此窗口关闭,则再次折叠所有编辑按钮...
用户控制
<StackPanel Orientation="Horizontal">
<Button Content="Edit" x:Name="edit" Height="50" Width="100" Margin="0" Click="Edit_Click" Visibility="{Binding editButtonVisibility}"/>
<Button Content="Show History" x:Name="Personal" Height="50" Width="100" Margin="0" Click="ShowHistory_Click" Visibility="{Binding ShowHistoryButtonVisibility}" />
<Button Content="Show Customer" x:Name="Customer" Height="50" Width="100" Margin="0" Click="ShowCustomer_Click" Visibility="{Binding ShowCustomerButtonVisibility}"/>
</StackPanel>
该用户控件的后台代码为上面的每个Bindings添加依赖属性:
public Visibility editButtonVisibility
{
get { return (Visibility)GetValue(editButtonVisibilityProperty); }
set { SetValue(editButtonVisibilityProperty, value); }
}
// Using a DependencyProperty as the backing store for editButtonVisibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty editButtonVisibilityProperty =
DependencyProperty.Register("editButtonVisibility", typeof(Visibility), typeof(UserControl1), new PropertyMetadata(Visibility.Visible));
public Visibility ShowHistoryButtonVisibility
{
get { return (Visibility)GetValue(ShowHistoryButtonVisibilityProperty); }
set { SetValue(ShowHistoryButtonVisibilityProperty, value); }
}
// Using a DependencyProperty as the backing store for ShowHistoryButtonVisibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShowHistoryButtonVisibilityProperty =
DependencyProperty.Register("ShowHistoryButtonVisibility", typeof(Visibility), typeof(UserControl1), new PropertyMetadata(Visibility.Visible));
public Visibility ShowCustomerButtonVisibility
{
get { return (Visibility)GetValue(ShowCustomerButtonVisibilityProperty); }
set { SetValue(ShowCustomerButtonVisibilityProperty, value); }
}
// Using a DependencyProperty as the backing store for ShowCustomerButtonVisibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShowCustomerButtonVisibilityProperty =
DependencyProperty.Register("ShowCustomerButtonVisibility", typeof(Visibility), typeof(UserControl1), new PropertyMetadata(Visibility.Visible));
该应用程序中的
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:UI_UserControls="clr-namespace:Anet.UI.UserControls;assembly=Anet.UI.UserControls"
xmlns:local="clr-namespace:test" x:Class="test.Window1"
Title="Window1" Height="300" Width="308">
<Grid>
<UI_UserControls: UserControl1 editButtonVisibility="Collapsed" ShowCustomerButtonVisibility="Collapsed" />
</Grid>
和窗口2和窗口3中的相同代码
我试图添加属性更改回调,但没有工作
public static readonly DependencyProperty editButtonVisibilityProperty =
DependencyProperty.Register("editButtonVisibility", typeof(Visibility), typeof(UserControl1), new PropertyMetadata(ProductionMenuChanged));
private static void ProductionMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UserControlBtu = d as UserControl1;
if (Window1.IsVisibleProperty == null)
{
UserControlBtu.edit.Visibility = Visibility.Collapsed;
}
else if (Window1.IsVisibleProperty != null)
{
UserControlBtu.edit.Visibility = Visibility.Visible;
}
}
P.S:用户控件不是本地..它是WPF用户控件库项目
答案 0 :(得分:0)
1.给UserControl一个名字
X:name="AnyName"
2.将按钮可见性属性绑定如下
Visibility="{Binding ElementName=AnyName, Path=editButtonVisibility}"
注意:用您的用户控件名替换AnyName。