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

Khattab al-Dakhil

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

用户控件

<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>

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

杰米p

您无需在Window_Loaded此处使用事件。

您需要Visibility为中的每个按钮公开一个属性UserControls

在您UserControlVisibility属性的每个按钮添加一个绑定

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

请确保您添加DataContext到您的UserControl,我一般都用自我:

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

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

在Visual Studio中,“依赖项属性”有一个代码片段快捷方式-键入propdp并单击tab两次。

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

<local:UserControl1 AddButtonVisibility="Collapsed" />

local是项目名称空间的别名-在您的顶部定义Window您只需将拖放UserControl到窗口上,它将为您完成此操作。(您可能需要重新构建才能UserControls在工具箱中看到您的内容。

现在,您应该可以看到已Add Button折叠的控件

为了完整起见,这是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:

<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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

单击主窗体上的按钮时,在用户控件中隐藏面板

从主窗口处理用户控件内的按钮单击

从用户控件访问主窗口

如何使用MVVM在WPF的主窗口中显示用户控件

如何从页面WPF禁用主窗口上的用户控件

从 WPF 中的主窗口引发用户控件上的事件

用户单击弹出窗口中的按钮后,如何在主活动中添加新按钮?

如何从用户控件的主窗体中读取属性

如何在videojs中隐藏/禁用按钮控件?

如何在C#中从用户控件隐藏标签?

如何在主窗口中向用户控件添加多个ViewModel

如何使用城堡(WPF)在主窗口中显示按路径注册的用户控件

WPF:如何在主窗口中使用用户控件的属性?

在主窗体 c# 中从用户控件创建媒体播放器按钮

如何检查是否单击了用户控件中的按钮?

如何关闭/隐藏应用主窗口?

在用户控件上设置绑定到主窗口的ViewModel

如何从用户控件内部关闭窗口?

如何从用户控件修改主机窗口?

如何显示登录窗口和隐藏主窗口

如何将用户控件上的按钮绑定到父控件中的命令?

通过按钮在wpf窗口中显示用户控件

从自定义用户控件中的父窗口引用控件

如何从主窗体内添加的模板用户控件中检测事件

如何隐藏/禁用GNOME窗口的关闭按钮?

如何从主窗口设置自定义控件的属性?

如何从Web窗体用户控件中的标记访问代码隐藏中定义的属性

如何在 SearchBar 控件 Xamarin 表单中隐藏清除按钮图标

如何在 tkinter 中向默认窗口管理器的控件添加新按钮?