4

Visibility プロパティに設定された ViewModel のプロパティを使用して、Expander のトグル ボタンを動的に非表示にしようとしています。少なくともそれが私の考えです。私のxamlでトグルボタンのテンプレート全体を実行することなく、トグルボタンコントロールの設定を変更する方法はありますか?

4

1 に答える 1

4

Loadedのイベントを使用して、イベント ハンドラーでExpanderをセットアップするBindingか、コード ビハインドなしで再利用可能な方法が必要な場合は、添付された動作を使用できます。

ToggleButton添付されたビヘイビアは の内部を見つけ、を添付されたプロパティにTemplate設定します。BindingToggleButtonVisibility

ここにサンプル アプリをアップロードしました: 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
}
于 2012-06-11T21:54:26.070 に答える