views:

612

answers:

4

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

+1  A: 

You can add a dependency property whose value is "the expander that is expanded," and then you can bind the "expanded" property to the expression "theExpandedProperty == this" using your favorite expression binding technique (type converter, etc).

Jon Watte
A: 

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

AWC
So, that's how you'd do it in an old-school framework like WinForms. It is not "the WPF way," though, because for WPF to be fully robust, you want to derive state from data, not from events.
Jon Watte
I realise this, hence my comments, but I'm happy with this in the code behind
AWC
A: 

I found an elegant, XAML-only implementation of this requirement. Click here!

Dabblernl
A: 

I'd just use a tab control, myself...

metao