Question

I have 2 expanders side by side. Only 1 can be opened at a time.

I want to write Triggers for them directly in their definition like this:

<Expander x:Name="MenuOverView" ExpandDirection="Left">                 
                <Expander.Triggers>
                    <Trigger Property="IsExpanded" Value="False" SourceName="MenuDetailed">
                                <Setter Property="IsExpanded" Value="True" TargetName="MenuOverView" /> 
                    </Trigger>
                </Expander.Triggers>
</Expander>

<Expander x:Name="MenuDetailed" ExpandDirection="Right">                 
                <Expander.Triggers>
                    <Trigger Property="IsExpanded" Value="False" SourceName="MenuOverView">
                                <Setter Property="IsExpanded" Value="True" TargetName="MenuDetailed" />
                    </Trigger>
                </Expander.Triggers>
   </Expander>

This is of course a bad code.. How can I do that please?

Was it helpful?

Solution

OK I think that's not possible. I provide a solution in that case:

<!--Add this to the resources dictionary-->
<conv:BoolInverterConverter x:Key="boolInvertorConverter" />

...

<Expander x:Name="MenuOverView" ExpandDirection="Left" 
                      IsExpanded="{Binding ElementName=MenuDetailed, Converter={StaticResource boolInvertorConverter}, Path=IsExpanded}"                      
                      VerticalContentAlignment="Center"  >  

...

and I provide the Converter code:

public class BoolInverterConverter : System.Windows.Data.IValueConverter
        {
            #region IValueConverter Members

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                bool i;
                if (bool.TryParse(value.ToString(), out i) == false) return null;            

                return !i;
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                bool i;
                if (bool.TryParse(value.ToString(), out i) == false) return null;

                return !i;
            }

            #endregion
        }

Enjoy!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top