我正在使用 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); }
}
- 1 回答
- 0 關注
- 115 瀏覽
添加回答
舉報
0/150
提交
取消