WPF |扩展事件

时间:2011-03-09 07:31:33

标签: wpf c#-3.0

在我的控制台中,我使用两个扩展器说(x& y)

默认设置为'IsExpanded = False'

一旦我点击x,那么y不应该是Expanded,反之亦然

所以请用代码回复我

3 个答案:

答案 0 :(得分:1)

即使可以在普通标记中进行,也可能更好地使用附加行为。这是一个例子:

class Toggle
    {
        Expander target;
        public Toggle(Expander src,Expander target)
        {
            this.target = target;
            src.Collapsed += new RoutedEventHandler(src_Collapsed);
            src.Expanded += new RoutedEventHandler(src_Expanded);
        }

        void src_Expanded(object sender, RoutedEventArgs e)
        {
            target.IsExpanded = false;
        }

        void src_Collapsed(object sender, RoutedEventArgs e)
        {
            target.IsExpanded = true;
        }
    }
    public static class ToggleExpanderBehavior
    {

        static List<Toggle> toggler = new List<Toggle>();
        public static Expander GetTargetExpander(DependencyObject obj)
        {
            return (Expander)obj.GetValue(TargetExpanderProperty);
        }

        public static void SetTargetExpander(DependencyObject obj, string value)
        {
            obj.SetValue(TargetExpanderProperty, value);
        }

        // Using a DependencyProperty as the backing store for TargetName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetExpanderProperty =
            DependencyProperty.RegisterAttached("TargetExpander", typeof(Expander), typeof(ToggleExpanderBehavior), new UIPropertyMetadata(null,new PropertyChangedCallback(OnTargetChanged)));

        static void OnTargetChanged(DependencyObject depo, DependencyPropertyChangedEventArgs depa)
        {
            if (depa.NewValue!=null)
            {
                if (depo is Expander)
                {
                    var exp = depo as Expander;
                    toggler.Add(new Toggle(exp,depa.NewValue as Expander));
                }
            }
        }




    }

您在xaml中附加此行为,如下所示:

 <Expander x:Name="X" IsExpanded="True" local:ToggleExpanderBehavior.TargetExpander="{Binding ElementName=Y}" >
            <TextBlock>Content 1</TextBlock>
        </Expander>
        <Expander x:Name="Y" IsExpanded="False"  local:ToggleExpanderBehavior.TargetExpander="{Binding ElementName=X}">

            <TextBlock>Content 2</TextBlock>
        </Expander>
    </StackPanel>

你已经开启了。

答案 1 :(得分:0)

这可能有所帮助:只需为所有扩展器项目注册扩展事件(所有相同的事件)。如果你打开一个,所有其他人都关闭了:

    private void expander_Expanded(object sender, RoutedEventArgs e)
    {
        SPExpander.Children.OfType<Expander>()
            .All<Expander>(expander =>
            {
                if ((Expander)sender != expander)
                    expander.IsExpanded = false;
                else
                    expander.IsExpanded = true;

                return true;
            });
    }

希望这能解决你的问题...

答案 2 :(得分:0)

我只想将一个扩展器的IsExpanded属性绑定到另一个扩展器的IsExpanded属性,然后你只需要提供一个反转值的值转换器

public class BoolInverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var b = value as bool?;
        return !b;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var b = value as bool?;
        return !b;
    }
}