通过绑定隐藏WPF扩展器上的ToggleButton

4

我正在尝试使用ViewModel上的属性来动态隐藏Expander中的Toggle Button,并将其设置为Visibility属性。至少这是我的想法。是否有一种方法可以仅更改ToggleButton控件上的一个设置,而不必在xaml中执行整个ToggleButton模板?

1个回答

4
你可以使用ExpanderLoaded事件,并在事件处理程序中设置Binding,或者如果你想要一个可重用的方式而不需要代码后台,则可以使用附加行为。

附加行为查找Template内部的ToggleButton并设置Binding到附加属性ToggleButtonVisibility

这里上传了一个示例应用程序:ExpanderToggleButtonVisibilityTest.zip

像这样使用:

<Expander Name="expander"
          behaviors:ExpanderBehavior.BindToggleButtonVisibility="True"
          behaviors:ExpanderBehavior.ToggleButtonVisibility="{Binding YourVisibilityProperty}"
          .../>

ExpanderBehavior

public class ExpanderBehavior
{
    public static DependencyProperty BindToggleButtonVisibilityProperty =
        DependencyProperty.RegisterAttached("BindToggleButtonVisibility",
                                            typeof(bool),
                                            typeof(ExpanderBehavior),
                                            new PropertyMetadata(false, OnBindToggleButtonVisibilityChanged));

    public static bool GetBindToggleButtonVisibility(Expander expander)
    {
        return (bool)expander.GetValue(BindToggleButtonVisibilityProperty);
    }
    public static void SetBindToggleButtonVisibility(Expander expander, bool value)
    {
        expander.SetValue(BindToggleButtonVisibilityProperty, value);
    }

    private static void OnBindToggleButtonVisibilityChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        Expander expander = target as Expander;
        if (expander.IsLoaded == true)
        {
            BindToggleButtonVisibility(expander);
        }
        else
        {
            RoutedEventHandler loadedEventHandler = null;
            loadedEventHandler = new RoutedEventHandler(delegate
            {
                BindToggleButtonVisibility(expander);
                expander.Loaded -= loadedEventHandler;
            });
            expander.Loaded += loadedEventHandler;
        }
    }

    private static void BindToggleButtonVisibility(Expander expander)
    {
        ToggleButton headerSite = expander.Template.FindName("HeaderSite", expander) as ToggleButton;
        if (headerSite != null)
        {
            Binding visibilityBinding = new Binding
            {
                Source = expander,
                Path = new PropertyPath(ToggleButtonVisibilityProperty)
            };
            headerSite.SetBinding(ToggleButton.VisibilityProperty, visibilityBinding);
        }
    }

    #region ToggleButtonVisibilityProperty

    public static DependencyProperty ToggleButtonVisibilityProperty = 
        DependencyProperty.RegisterAttached("ToggleButtonVisibility",
                                            typeof(Visibility),
                                            typeof(ExpanderBehavior),
                                            new PropertyMetadata(Visibility.Visible));

    public static Visibility GetToggleButtonVisibility(Expander expander)
    {
        return (Visibility)expander.GetValue(ToggleButtonVisibilityProperty);
    }
    public static void SetToggleButtonVisibility(Expander expander, Visibility value)
    {
        expander.SetValue(ToggleButtonVisibilityProperty, value);
    }

    #endregion // ToggleButtonVisibilityProperty
}

谢谢。我会考虑尝试做这个。 - g.t.w.d
除了重新模板化之外,您还有另一个选项。您可以子类化“Expander”并在“OnApplyTemplate”中查找“ToggleButton”。我现在无法访问我的计算机,因此无法给出示例,但您可以在此处找到类似的内容:https://dev59.com/dFTTa4cB1Zd3GeqPuaiC - Fredrik Hedblad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接