FantomVII
FantomVII

Reputation: 31

Collapsing/expanding action with wpf buttons

First off, let me state I am an amateur when it comes to wpf. I am trying to create an a collapsing/expanding type action for a wpf button, meaning when a user clicks a button, I would like the button selected to expand a new list of buttons beneath it. This is meant to be the navigation type for the web-enabled application. I would also like to create a collapsing interaction when the button is pressed again on an opened list.

Upvotes: 3

Views: 5239

Answers (3)

brunnerh
brunnerh

Reputation: 185290

Just use a ToggleButton and bind the visibility of the section to its IsChecked state as normally done in the Expander control (which you of course could just use instead).

Upvotes: 1

MatthiasG
MatthiasG

Reputation: 4532

My first idea is to use a ToggleButton and bind its IsChecked property to a the visibility of the element you want to show. You would need a converter then to convert the boolean Checked value to a Visibility value. Here is an example:

<Grid>
  <Grid.Resources>
    <BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
  </Grid.Ressources>
  <ToggleButton x:Name="toggleButton" Content="Toggle" />
  <Grid Visibility={Binding IsChecked, ElementName=toggleButton, Converter={StaticResource BoolToVisibilityConverter}}>
    <!-- place your content here -->
  </Grid>
</Grid>

The converter is a class implementig IValueConverter:

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool i = value is bool ? (bool) value : false;
        return i ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 2

Bas
Bas

Reputation: 27105

There is a default control for this in WPF, named the Expander. If you want to change the appearance or the animations you could look into templating and styling of WPF. By default this control should meet most of your requirements.

Upvotes: 5

Related Questions