简体   繁体   English

如何从主窗口隐藏用户控件中的按钮?

[英]how to Hide buttons in usercontrol from main window?

I have a user control that have a multi buttons and In the application i use this user control on multi windows ,but i want to Collapsed (shown/hidden) some buttons if the user select in the application a window 1 and show same button if the user select in the application a window 2 我有一个具有多个按钮的用户控件,在应用程序中,我在多个窗口上使用了该用户控件,但是如果用户在应用程序中选择窗口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>

how to set the property (Visibility) of button in the User Control from the application window ? 如何从应用程序窗口设置用户控件中按钮的属性(可见性)?

You don't need to use a Window_Loaded Event here. 您无需在此处使用Window_Loaded事件。

You need to expose a Visibility property for each of your buttons in your UserControls . 您需要为UserControls每个按钮公开一个Visibility属性。

In your UserControl add a binding to each button for the Visibility property: 在您的UserControl ,为Visibility属性的每个按钮添加一个绑定:

Visibility="{Binding AddButtonVisibility}"
Visibility="{Binding ShowHistoryButtonVisibility}"
Visibility="{Binding ShowCustomerButtonVisibility}"

Make sure you add a DataContext to your UserControl , I generally use Self: 确保将DataContext添加到UserControl ,我通常使用Self:

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

In your UserControl Code Behind add Dependency Properties for each of the Bindings above: 在后面的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));

In Visual Studio, There is a code snippet shortcut for Dependency Properties - type propdp and hit tab twice. 在Visual Studio中,“依赖项属性”有一个代码片段快捷方式-键入propdp并单击两次tab

Now, to use the properties you have just created put the Usercontrol onto the relevant window: 现在,要使用刚刚创建的属性,请将Usercontrol放到相关窗口中:

<local:UserControl1 AddButtonVisibility="Collapsed" />

local is the project namespaces' alias - defined at the top of your Window . local是项目名称空间的别名-在Window的顶部定义。 You can just drag and drop the UserControl onto your window, and it will do this for you. 您只需将UserControl拖放到窗口上,即可为您完成此操作。 (You may need to rebuild in order to see your UserControls in your Toolbox. (您可能需要重新构建才能在工具箱中查看UserControls

You should now see your control with the Add Button collapsed. 现在,您应该看到折叠了“ Add Button控件。

For completeness sake, here is the XAML side of things: 为了完整起见,这是XAML方面的内容:

UserControl Xaml: UserControl Xaml:

<UserControl x:Class="WpfApplication2.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid x:Name="girdBtuWidow" >
    <StackPanel Orientation="Horizontal">
        <Button Content="Add" x:Name="add" Height="50"  Width="100" Margin="0" Click="Add_Click" Visibility="{Binding AddButtonVisibility}"/>
        <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>
</Grid>

Window1.Xaml: Window1.Xaml:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2" x:Class="WpfApplication2.Window1"
    Title="Window1" Height="300" Width="308">
<Grid>
    <local:UserControl1 HorizontalAlignment="Left" VerticalAlignment="Top" AddButtonVisibility="Collapsed" />
</Grid>

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

//After this goto xaml //此goto xaml之后

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM