如何从主窗口隐藏usercontrol中的按钮?

时间:2015-11-17 10:57:45

标签: c# wpf user-controls

我有一个具有多个按钮的用户控件,在应用程序中我在多个窗口上使用此用户控件,但是如果用户在应用程序中选择一个窗口1并显示,我想要折叠(显示/隐藏)一些按钮如果用户在应用程序中选择窗口2

,则使用相同的按钮

UserControl

<Grid x:Name="girdBtuWidow" >
    <StackPanel Orientation="Horizontal">
        <Button Content="add" x:Name="add" Visibility="{Binding window1_Loaded}" x:FieldModifier="public" Height="50"  Width="100" Margin="0"  Click="add_click"  />
        <Button Content="show history" x:Name="Personal" Height="50"  Width="100" Margin="0" />
        <Button Content="Show Customer" x:Name="Customer" Height="50"  Width="100" Margin="0" />
    </StackPanel>
</Grid>

如何从应用程序窗口设置用户控件中按钮的属性(可见性)?

4 个答案:

答案 0 :(得分:1)

您不需要在此处使用module Messages open Bizmonger.Patterns let Publish (message:string, payload:_) = MessageBus.Publish(message, payload) 活动。

您需要为open Messages ... Publish("SOME_MESSAGE", null); 中的每个按钮公开Window_Loaded属性。

Visibility中为UserControls属性的每个按钮添加绑定:

UserControl

请务必向Visibility添加Visibility="{Binding AddButtonVisibility}" Visibility="{Binding ShowHistoryButtonVisibility}" Visibility="{Binding ShowCustomerButtonVisibility}" ,我通常使用自我:

DataContext

UserControl代码后面为上面的每个Bindings添加依赖属性:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

在Visual Studio中,依赖项属性有一个代码段快捷方式 - 键入UserControl并点击 public Visibility AddButtonVisibility { get { return (Visibility)GetValue(AddButtonVisibilityProperty); } set { SetValue(AddButtonVisibilityProperty, value); } } // Using a DependencyProperty as the backing store for AddButtonVisibility. This enables animation, styling, binding, etc... public static readonly DependencyProperty AddButtonVisibilityProperty = DependencyProperty.Register("AddButtonVisibility", 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)); 两次。

现在,要使用刚刚创建的属性,请将Usercontrol放到相关窗口中:

propdp

tab是项目命名空间&#39;别名 - 在<local:UserControl1 AddButtonVisibility="Collapsed" /> 的顶部定义。您只需将local拖放到窗口即可,它将为您执行此操作。 (您可能需要重建才能在工具箱中看到Window

现在,您应该会看到UserControl崩溃的控件。

为了完整起见,这里是UserControls方面的事情:

UserControl Xaml:

Add Button

Window1.Xaml:

XAML

答案 1 :(得分:0)

public static readonly DependencyProperty onBackVisibilityProperty =
     DependencyProperty.Register("onBackVisibility", typeof(Visibility), typeof(MyToolBar), new PropertyMetadata(Visibility.Visible));
    public Visibility onBackVisibility
    {
        get { return (Visibility)GetValue(onBackVisibilityProperty); }
        set { SetValue(onBackVisibilityProperty, value); }
    }

//在此goto xaml之后

答案 2 :(得分:0)

public static readonly DependencyProperty onBackVisibilityProperty =
     DependencyProperty.Register("onBackVisibility", typeof(Visibility), typeof(MyToolBar), new PropertyMetadata(Visibility.Visible));
    public Visibility onBackVisibility
    {
        get { return (Visibility)GetValue(onBackVisibilityProperty); }
        set { SetValue(onBackVisibilityProperty, value); }
    }

答案 3 :(得分:0)

Button Name="tbrBack" 
ToolTip="{DynamicResource Back}" 
VerticalAlignment="Center" 
VerticalContentAlignment="Center" 
Click="tbrBack_Click" 
Visibility="{Binding onBackVisibility ,ElementName = usercontrol}