为了账号安全,请及时绑定邮箱和手机立即绑定

单向依赖属性的实现

单向依赖属性的实现

C#
慕沐林林 2023-09-16 14:56:25
我正在使用 WPF 开发自定义用户控件。我已经注册了 DependancyProperty,但我想让它仅为 OneWay 绑定。有可能做到吗?这是我所拥有的:public static readonly DependencyProperty CustomPrProperty =        DependencyProperty.Register(            "CustomPr",            typeof(string),            typeof(CustomView),            new FrameworkPropertyMetadata(string.Empty, OnDependencyPropertyChanged));这样,当有人使用用户控件时,他可以将其设为OneWay、OneWayToSource 和TwoWay。我怎样才能使其只读属性?
查看完整描述

1 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

您可以设置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); }

}


查看完整回答
反对 回复 2023-09-16
  • 1 回答
  • 0 关注
  • 53 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信