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

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条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

JFrame:如何在单击按钮时隐藏主窗口?

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

使用主窗体c#中的按钮在用户控件中调用事件

来自分类Dev

动态创建的用户控件,可从主窗体中检测按钮单击

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

如何显示一个用户控件并将其他控件隐藏在C#中

来自分类Dev

如何根据aspx页面中的查询字符串隐藏用户控件

来自分类Dev

如何在Android中启动屏幕后隐藏主屏幕上的后退按钮?

来自分类Dev

如何将字符串从用户控件传递到WPF中的父窗口?

来自分类Dev

如何使用KeyPresses在没有窗口的ElementHost中托管WPF用户控件

来自分类Dev

如何在Visual Studio 2013的“主窗口框架控制”窗格中添加按钮?

来自分类Dev

如何在Visual Studio 2013的“主窗口框架控制”窗格中添加按钮?

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

    JFrame:如何在单击按钮时隐藏主窗口?

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

    使用主窗体c#中的按钮在用户控件中调用事件

  9. 9

    动态创建的用户控件,可从主窗体中检测按钮单击

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

    如何显示一个用户控件并将其他控件隐藏在C#中

  24. 24

    如何根据aspx页面中的查询字符串隐藏用户控件

  25. 25

    如何在Android中启动屏幕后隐藏主屏幕上的后退按钮?

  26. 26

    如何将字符串从用户控件传递到WPF中的父窗口?

  27. 27

    如何使用KeyPresses在没有窗口的ElementHost中托管WPF用户控件

  28. 28

    如何在Visual Studio 2013的“主窗口框架控制”窗格中添加按钮?

  29. 29

    如何在Visual Studio 2013的“主窗口框架控制”窗格中添加按钮?

热门标签

归档