亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單向依賴屬性的實現

單向依賴屬性的實現

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 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號