单向依赖属性的实现

我正在使用 WPF 开发自定义用户控件。我已经注册了 DependancyProperty,但我想让它仅为 OneWay 绑定。有可能做到吗?这是我所拥有的:


public static readonly DependencyProperty CustomPrProperty =

        DependencyProperty.Register(

            "CustomPr",

            typeof(string),

            typeof(CustomView),

            new FrameworkPropertyMetadata(string.Empty, OnDependencyPropertyChanged));

这样,当有人使用用户控件时,他可以将其设为OneWay、OneWayToSource 和TwoWay。我怎样才能使其只读属性?


慕沐林林
浏览 59回答 1
1回答

忽然笑

您可以设置BindsTwoWayByDefault的属性FrameworkPropertyMetadata来指定该属性默认绑定双向。Mode仍然可以通过将单个绑定的属性设置为 以外的其他内容来更改模式TwoWay。要创建无法设置的只读依赖属性,您应该使用RegisterReadOnly方法:internal static readonly DependencyPropertyKey CustomPrKey = DependencyProperty.RegisterReadOnly( "CustomPr", typeof(string), typeof(CustomView), new PropertyMetadata(string.Empty));public static readonly DependencyProperty CustomPrProperty = CustomPrKey.DependencyProperty;public string CustomPr{    get { return (string)GetValue(CustomPrProperty); }}
打开App,查看更多内容
随时随地看视频慕课网APP