Thomas
Thomas

Reputation: 305

collapse all buttons in a class

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:

XAML

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

and

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

    cmd_Button2.Visibility = Visibility.Visible;
}

Upvotes: 2

Views: 155

Answers (3)

Justin CI
Justin CI

Reputation: 2741

you can use

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

and call

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

xaml is

<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>

Upvotes: 0

Jose
Jose

Reputation: 1897

You can set a style with trigger to set the behaviour of a group of Buttons(or UIElements in general):

Button style(on 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>

Usage on your buttons:

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

And, as GazTheDestroyer said, using MVVM, define a boolean in your datacontext, called CollapseButtonsBoolean. Your Buttons Visibility will change at the same time that the boolean.

Upvotes: 0

GazTheDestroyer
GazTheDestroyer

Reputation: 21261

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.

Upvotes: 1

Related Questions