1
在我的應用程序中,我想阻止其他程序員爲我的Usercontrols設置屬性,這可能會導致一些麻煩。如何禁用爲控件設置屬性的可能性?
一個簡單的例子是,我不想讓某人設置UseLayoutRounding
爲true。
<Button UseLayoutRounding="True"/>
我想禁用Intelisense
顯示我UseLayoutRounding
。
在我的應用程序中,我想阻止其他程序員爲我的Usercontrols設置屬性,這可能會導致一些麻煩。如何禁用爲控件設置屬性的可能性?
一個簡單的例子是,我不想讓某人設置UseLayoutRounding
爲true。
<Button UseLayoutRounding="True"/>
我想禁用Intelisense
顯示我UseLayoutRounding
。
,你可以覆蓋OnPropertyChanged
方法,當有人試圖去改變它拋出異常:
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (e.Property == UseLayoutRoundingProperty && (bool)e.NewValue)
{
throw new PropertyIsImmutableException("UseLayoutRounding");
}
//....
base.OnPropertyChanged(e);
}
或者你可以使用new
關鍵字:
public new bool UseLayoutRounding
{
get { return (bool)GetValue(UseLayoutRoundingProperty); }
}
但是,使用第二形式給出不先保持posibility到改變這樣的值:
yourSuperControl.SetValue(SuperControlType.UseLayoutRoundingProperty, true);
那就是功能好吧,但它不是「隱藏的屬性」的解決方案 – Bulli 2012-08-17 08:22:40
爲什麼?使用'new'關鍵字隱藏舊屬性時,setter將被禁用,並且不會顯示IntelliSence。據我所知隱藏財產是不可能的,這是最好的解決方案 – dvvrd 2012-08-17 08:28:52