折叠类中的所有按钮

本文关键字:按钮 折叠 | 更新日期: 2023-09-27 18:26:55

我是C#的新手,需要一些帮助。我试过在谷歌上搜索,但没有用,所以我在这里询问。我有带命令按钮的WPF表单。我使用:

cmd_Button.Visibility = Visibility.Collapsed;
cmd_Button.Visibility = visibility.Visible;

根据需要隐藏和显示按钮。问题是我有很多按钮,我必须在表单初始化时折叠所有按钮(除了三个),我必须折叠所有按钮,除了我想在所有Click事件上显示的按钮。

我的问题是;有没有办法在一个类的开头折叠所有按钮,然后用visibility.visible显示我想显示的按钮?

以下是我使用的代码示例:

XAML

<Button x:Name="cmd_Button1" Content="Button1" HorizontalAlignment="Left" Margin="15,96,0,0" VerticalAlignment="Top" Width="75" Click="cmd_Button1_Click"/>

然后c#

public MainWindow()
    {
        InitializeComponent();
        //collapse buttons
        cmd_Button1.Visibility = Visibility.Collapsed;
        cmd_Button2.Visibility = Visibility.Collapsed;
        cmd_Button3.Visibility = Visibility.Collapsed;
}

private void cmd_ShowButton2_Click(object sender, RoutedEventArgs e)
{
    cmd_Button1.Visibility = Visibility.Collapsed;
    cmd_Button3.Visibility = Visibility.Collapsed;
    cmd_Button2.Visibility = Visibility.Visible;
}

折叠类中的所有按钮

您可以在页面资源中创建一个样式,将按钮可见性默认为折叠:

 <UserControl.Resources>
     <Style TargetType="Button">
         <Setter Property="Visibility" Value="Collapsed" />                  
     </Style>
 </UserControl.Resources>

不过,我敦促您阅读有关MVVM的内容。WPF使用MVVM绑定比在代码背后操作ui元素更干净、更简单。

您可以设置一个带有触发器的样式来设置一组按钮(或通常的UIElements)的行为:

按钮样式(在Usercontrols.Resources上):

<Style x:Key="CollapsableButtons" TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding CollapseButtonsBoolean}" Value="true">
            <Setter Property="Visibility" Value="Collapsed" />
        </DataTrigger>
    </Style.Triggers>
</Style>

按钮上的用法:

<Button Style="{StaticResource CollapsableButtons}"/>

正如GazTheDestroyer所说,使用MVVM,在数据上下文中定义一个布尔值,称为CollapseButtonsBoolean。您的按钮可见性将与布尔值同时更改。

您可以使用

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }
                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

并呼叫

 foreach (Button tb in FindVisualChildren<Button>(testWindow))
            {
               tb.Visibility = Visibility.Collapsed;
            }

xaml是

<Window x:Class="WpfApplication1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cef="clr-namespace:WpfApplication1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="Window1" x:Name="testWindow" Height="300" Loaded="Window_Loaded" Width="300" WindowStyle="None">
    <Grid Background="White">
        <Button x:Name="but1" Content="but1"></Button>
        <Button x:Name="but2" Content="but2"></Button>
        <Button x:Name="but3" Content="but3"></Button>

    </Grid>
</Window>