Akhil
Akhil

Reputation: 471

Hide All buttons in a WPF window

I am new to WPF. I have a window with multiple buttons. I would like to hide all buttons in the window except for one. Buttons are added to the window dynamically.

XAML Code:

<Grid>
<Button x:Name="btnA"/>
<Button x:Name="btnB" />
<Button x:Name="btnC"/>
<Button x:Name="btnD" />
<Button x:Name="btnE"/>
<Button x:Name="btnF" />
<Button x:Name="btnG"/>
<Button x:Name="btnH" />
    <StackPanel >
         <Button x:Name="btnHideAllButtons" click="btnHideAllButtons_Click"/>
    </StackPanel>
</Grid>

C# Code

 private void btnHideAllButtons_Click(object sender, RoutedEventArgs e)
        {
           //Code to Hide all Buttons

            btnHideAllButtons.Visibility = Visibility.Visible;
        }

Upvotes: 1

Views: 2523

Answers (4)

Salvatore Gangemi
Salvatore Gangemi

Reputation: 1

You can set the visibility of the button from the property->aspect->visibility. When you want to make the button visible, you can write:

Button.Visibility = Visibility.Visible;

Upvotes: 0

romanoza
romanoza

Reputation: 4862

You can find all of the buttons that are children of the gridMain grid in the following way:

Xaml:

<Grid Name="gridMain">
    <Button x:Name="btnA"/>
    <Button x:Name="btnB" />
    <Button x:Name="btnC"/>
    <Button x:Name="btnD" />
    <Button x:Name="btnE"/>
    <Button x:Name="btnF" />
    <Button x:Name="btnG"/>
    <Button x:Name="btnH" />
    <StackPanel >
        <Button x:Name="btnHideAllButtons" Click="btnHideAllButtons_Click" Content="Hide"/>
    </StackPanel>
</Grid>

Code:

private void btnHideAllButtons_Click(object sender, RoutedEventArgs e) {
    gridMain.Children.OfType<Button>()
        .ToList()
        .ForEach(b => b.Visibility = Visibility.Collapsed);
}

Upvotes: 1

Lou Watson
Lou Watson

Reputation: 81

2 possible solutions I can think of:

1: Put all the buttons you want to hide inside a stackpanel or similar element and hide that. Of course this would mean the one to be kept visible would have to be kept in an element outside the rest.

2: Use Binding such as:

In your code behind / ViewModel put a property like so

public Visibility ButtonVisibility { get; set; }

and set it in the clas constructor ie:

ButtonVisibility = Visibility.Visible;

and for each button you want to hide set the binding in xaml:

<Button ... Visibility="{Binding ButtonVisibility}">

then change the property to hide like so:

ButtonVisibility = Visibility.Collapsed;

Upvotes: 2

Tyress
Tyress

Reputation: 3653

Put all your buttons in a List:

public List<Button> allButtons; //(declared on your Window's cs)

//Initialize on your constructor
allButtons = new List<Button>() {btnA,btnB,/*and so on*/}

and then do a foreach

private void btnHideAllButtons_Click(object sender, RoutedEventArgs e)
{
   foreach (Button button in allButtons)
   {
        button.Visibility = Visibility.Collapsed;
   }
   btnHideAllButtons.Visibility = Visibility.Visible;
}

Upvotes: 2

Related Questions