Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - How to get only one expander expanded at any one time

I've got a StackPanel with a group of expanders in, how do I set it so that only expander is expanded at any one time?

Cheers

AW

like image 670
AwkwardCoder Avatar asked Dec 17 '09 15:12

AwkwardCoder


1 Answers

I didn't really want to do it like this as it required putting code (C#) in the class behind file for the window (I'm trying to avoid this completely by use of ViewModels etc).

Ideally I would have described this in XAML.

I hooked up every Expander 'Expanded' event I was interested in and did the following:

    private void HandleExpanderExpanded(object sender, RoutedEventArgs e)
    {
        ExpandExculsively(sender as Expander);
    }

    private void ExpandExculsively(Expander expander)
    {
        foreach (var child in findPanel.Children)
        {
            if (child is Expander && child != expander)
                ((Expander)child).IsExpanded = false;
        }
    }

Cheers

AWC

like image 190
AwkwardCoder Avatar answered Oct 06 '22 22:10

AwkwardCoder