Misters
Misters

Reputation: 1347

Get DependencyProperty Owner Type instance on value change

I created a dependency property inside my ActionBar custom control:

 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
    }

I have a callback that when the value of that property change, I have to edit the ActionBar style(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;
                    }
                };
            }
        }

But my question is, how can i get the instance of the ActionBar Control(inherits from flyout) inside of PropertyChangeCallback to edit its style?

Upvotes: 2

Views: 949

Answers (1)

Ponas Justas
Ponas Justas

Reputation: 305

Firstly, change your dependency property declaration.

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

Then add a method ValueChanged to your ActionBar custom control class.

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;
    }
}

And lastly modify your NavigationStylePropertyMetadata constructor to accept 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
        }

Actually there is no need to have a custom class for property metadata. Just declare your dependency property like this:

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

Upvotes: 3

Related Questions