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

标签 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 样式(宽度):

 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 Control( inherits from flyout ) 实例来编辑其样式?

最佳答案

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

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

关于c# - 在值更改时获取 DependencyProperty Owner Type 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37488119/

相关文章:

wpf - 如何在WPF密码框中显示几秒钟的字符?

c# - PRISM中的复合 View 导航

C# WPF 单选按钮拆分到不同的页面

wpf - 你能清除触发器中的依赖属性吗?

mvvm - UWP 属性在用户控件的依赖属性中更改

c# - 模型绑定(bind)到 Nancy 中的 Dictionary<string,string>

c# - Linux 上的 .NET 核心 X509Store

c# - C# 中的 "yield"和 "yield return"有什么区别(如果有的话)?

c# - LINQ 到 SQL : Why can't I have a Zero to Many Cardinality

c# - WPF双向绑定(bind)到属性的属性以替换父属性