1

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;
        }
Tyress
  • 3,573
  • 2
  • 22
  • 45
Akhil
  • 441
  • 7
  • 24
  • Bryce's answer here: http://stackoverflow.com/a/978352/1723823 provides a simple and ugly way to iterate over the visual tree and find all buttons in your window, at which point you can hide them in a foreach as he demonstrates. This is not a good way to do it, though. In an ideal world, you should either group your buttons in the UI inside a parent control you can hide (thus hiding them all at once), or at least name them and hide them one by one in code. As mentioned below, created buttons should have references to them stored in a list (or be put in a parent control) and hidden that way. – TernaryTopiary Feb 26 '16 at 07:24

4 Answers4

2

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;
}
Tyress
  • 3,573
  • 2
  • 22
  • 45
  • But how can we, also list the buttons which are also not added dynamically on to the window?? – Akhil Feb 26 '16 at 07:16
  • Well first of all if it's on the XAML I wouldn't say it's dynamically created;). If you are created buttons during runtime (that is what I would say is dynamic), you can always add it to the list everytime after you create them using `allButtons.Add(dynamicButtonA);` or something – Tyress Feb 26 '16 at 07:18
2

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;
Lou Watson
  • 81
  • 1
  • 6
1

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);
}
romanoza
  • 4,775
  • 3
  • 27
  • 44
0

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;