Expand / Collapse all expanders

I have a list of extenders that I want to control with their expanded state (IsExpanded) using the global toggle button, which should switch between the expanded / collapsed state.

The solution I got so far does this by linking the Expander Expander state to the IsChecked togglebutton state. This works until I manually switch the expanders. As soon as I do this, these specific expanders do not respect the binding (switch the "Fixed" button).

Any idea why? and is there a solution for this in XAML?

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
  <StackPanel>
    <ToggleButton Name="ExpandAll">Toggle</ToggleButton>  
    <Expander IsExpanded="{Binding ElementName=ExpandAll,Path=IsChecked, Mode=OneWay}">
      Hai
    </Expander>
    <Expander IsExpanded="{Binding ElementName=ExpandAll,Path=IsChecked, Mode=OneWay}">
      Hello
    </Expander>
    <Expander IsExpanded="{Binding ElementName=ExpandAll,Path=IsChecked, Mode=OneWay}">
      Weird
    </Expander>
    </StackPanel>
  </Grid>
</Page>
+3
source share
2 answers

, XAML, IValueConverter:

<StackPanel>
        <StackPanel.Resources>
            <local:Converter x:Key="Converter" />
        </StackPanel.Resources>
        <ToggleButton Name="ExpandAll">
            <ToggleButton.IsChecked>
                <MultiBinding Mode="OneWayToSource" Converter="{StaticResource Converter}">
                    <Binding ElementName="Expander1" Path="IsExpanded" />
                    <Binding ElementName="Expander2" Path="IsExpanded" />
                    <Binding ElementName="Expander3" Path="IsExpanded" />
                </MultiBinding>
            </ToggleButton.IsChecked>
            Toggle</ToggleButton>
        <Expander Name="Expander1">
            Hai
        </Expander>
        <Expander Name="Expander2">
            Hello
        </Expander>
        <Expander Name="Expander3">
            Weird
        </Expander>
    </StackPanel>

Converter :

public class Converter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //we're using OneWayToSource, so this will never be used.
        return DependencyProperty.UnsetValue;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        // we want to convert the single 'IsChecked' value from the ToggleButton into 
        // 3 'IsExpanded' values
        var allValues = new object[targetTypes.Length];
        for (int i = 0; i < allValues.Length; i++)
        {
            allValues[i] = value;
        }

        return allValues;
    }
}

, OneWayToSource IsChecked ToggleButton (.. ​​ ) IMultiValueConverter Expander s.

+2

, . , . .

<Expander IsExpanded="{Binding ElementName=ExpandAll, Path=IsChecked, UpdateSourceTrigger=Explicit}">
</Expander>

, , DataGrid.RowDetailsTemplate.

+7

Source: https://habr.com/ru/post/1773812/


All Articles