3

collapses all buttons in a class

 2 years ago
source link: https://www.codesd.com/item/collapses-all-buttons-in-a-class.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

collapses all buttons in a class

advertisements

Im new to C# and need a little help. I have tried googling, but to no avail so I'm asking here. I have WPF form with command buttons. And I use:

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

To hide and show buttons as needed. The problem is that I have a lot of buttons and I have to collapse all(except three) at the initialization of the form and I have to collapse all except the ones I want to show on all Click events.

My question is; is there any way to collapse all buttons in a class, at the beginning of said class, and then show the ones i want to show with visibility.visible?

Here is an example of the code I am using:

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

and then 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;
}


You could create a style in your page resource that defaults button visibility to collapsed:

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

I would urge you to read up about MVVM though. WPF is far cleaner and simpler to use using MVVM bindings rather than manipulating ui elemenents in code behind.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK