开发者

WPF Expanders Triggers

开发者 https://www.devze.com 2022-12-09 00:46 出处:网络
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:

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 Prope开发者_高级运维rty="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?


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消