1

我并排有 2 个扩展器。一次只能打开 1 个。

我想直接在他们的定义中为他们编写触发器,如下所示:

<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>

这当然是一个糟糕的代码。请问我该怎么做?

4

1 回答 1

3

好的,我认为这是不可能的。在这种情况下,我提供了一个解决方案:

<!--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"  >  

...

我提供转换器代码:

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
        }

享受!

于 2009-10-14T14:01:16.903 回答