views:

54

answers:

2

What I want to achieve: To collapse or expand all Expanders that share the same group name within a group style.

We have a ListBox and another ListBox nested inside it to display child items. The child items ItemsSource is bound to a CollectionView with Group descriptions attached.

The group item template is quite simple:

<Expander IsExpanded="{Binding Path=WHAT_TO_DO, Mode=TwoWay}">
                <Expander.Header>
                    <TextBlock Text="{Binding Name}" />
                </Expander.Header>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" SharedSizeGroup="{Binding Name, Converter={StaticResource GroupNameToUniqueNameConverter}}" />
                    </Grid.RowDefinitions>
                    <ItemsPresenter/>
                </Grid>
            </Expander>
+1  A: 

I don't think this can be done so easily. There are probably two options here

1) Manually - bind every expander that you need to eg. the first item in the group or anything else that is appropriate.

2) More automatic way would probably require writing a class that has a dictionary that holds states for each group. The problem here is that the controller needs to know which instance of expander is doing the binding or which group it is in. One solution that I see here is writing a custom converter that has a static instance of the class with dictionary and using the converter parameter to pass the group/reference (you can use binding here, so in xaml this is pure Ctrl+C, Ctrl+V operation)

kubal5003
We were actually trying a solution that was something like your second option, but I can't bind the group name with a normal {Binding} since it is dynamic of the datacontext I think? We tried to pass the group name and the dictionary (in the PM) to a converter in a multibinding but there was some issues in the convertback method since we don't know the Group name.
David
Bindings work for dynamic things too, I don't really see the problem right now. I'll take a closer look at it later and try to build a working example.
kubal5003
Sorry, was a bit unclear. It is not possible to use a Binding to pass a dynamic ConverterParameter to a Converter.
David
A: 

You can in code behind iterate over all listbox items. For each listbox item you should see if it contains an expander. If it does you just expand or collapse it. See this link for how to iterate over items and finding a specific control.

http://stackoverflow.com/questions/693848/is-there-a-way-to-iterate-in-a-listbox-items-templates

Wallstreet Programmer
This is probably the worst working solution.
kubal5003
Worst working solution because it is a general solution which can be reused? Worst working solution because it handles view related matters in the view without messing up your controller with view specific code?
Wallstreet Programmer