簡體   English   中英

通過Binding在WPF Expander上隱藏ToggleButton

[英]Hide the ToggleButton on WPF Expander via Binding

我試圖使用設置為Visibility屬性的ViewModel上的屬性動態隱藏Expander上的Toggle Button。 至少這是我的想法。 有沒有辦法只需更改ToggleButton控件上的那個設置,而無需在我的xaml中執行切換按鈕的整個模板?

您可以使用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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM