在值更改时获取DependencyProperty Owner Type实例

时间:2016-05-27 16:14:24

标签: c# wpf dependency-properties

我在ActionBar自定义控件中创建了一个依赖项属性:

 public NavigationStyle NavigationStyle
        {
            get { return (NavigationStyle)GetValue(NavigationStyleProperty); }
            set { SetValue(NavigationStyleProperty, value); }
        }


        // Using a DependencyProperty as the backing store for NavigationStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NavigationStyleProperty =
            DependencyProperty.Register("NavigationStyle", typeof(NavigationStyle), typeof(ActionBar), new 

NavigationStylePropertyMetadata(NavigationStyle.TwoColumnsNavigation));

 public enum NavigationStyle
    {
        SingleNavigation,
        TwoColumnsNavigation
    }

我有一个回调,当该属性的值发生变化时,我必须编辑ActionBar样式(widht):

 private class NavigationStylePropertyMetadata : FrameworkPropertyMetadata
        {
            public NavigationStylePropertyMetadata(object defaultValue)
                :base(defaultValue)
            {

                base.PropertyChangedCallback = (dependicyProperty, e) => {

// How can i get the instance of the ActionBar control ?
                    switch ((NavigationStyle)e.NewValue)
                    {
                        case NavigationStyle.SingleNavigation:
                        // here i need to edit the width of the ActionBar to 500px
                            break;
                        case NavigationStyle.TwoColumnsNavigation:
// and here i have to edit the ActionBar width to 700
                            break;
                        default:
                            break;
                    }
                };
            }
        }

但我的问题是,如何在PropertyChangeCallback内部获取ActionBar控件(inherits from flyout)的实例来编辑其样式?

1 个答案:

答案 0 :(得分:2)

首先,更改您的依赖项属性声明。

public static readonly DependencyProperty NavigationStyleProperty =
        DependencyProperty.Register("NavigationStyle", typeof(NavigationStyle), typeof(ActionBar), new NavigationStylePropertyMetadata(NavigationStyle.TwoColumnsNavigation, ValueChanged));

然后将方法ValueChanged添加到ActionBar自定义控件类。

private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ActionBar actionBar = d as ActionBar;
    // Do other stuff
    switch ((NavigationStyle)e.NewValue)
    {
        case NavigationStyle.SingleNavigation:
            // here i need to edit the width of the ActionBar to 500px
            break;
        case NavigationStyle.TwoColumnsNavigation:
            // and here i have to edit the ActionBar width to 700
            break;
        default:
            break;
    }
}

最后修改您的NavigationStylePropertyMetadata构造函数以接受PropertyChangedCallback

 public NavigationStylePropertyMetadata(object defaultValue, PropertyChangedCallback propertyChangedCallback)
            :base(defaultValue, propertyChangedCallback)
        {
            // do some stuff here, or just remove this class if you dont need it and just use FrameworkPropertyMetadata
        }

实际上,不需要为属性元数据设置自定义类。只需声明你的依赖属性:

public static readonly DependencyProperty NavigationStyleProperty =
    DependencyProperty.Register(
        "NavigationStyle",
        typeof(NavigationStyle),
        typeof(ActionBar),
        new FrameworkPropertyMetadata(
            NavigationStyle.TwoColumnsNavigation, ValueChanged));