有没有办法在WPF中创建自定义属性?
当我的设计师中的自定义属性未填充时,我的意思是错误消息?例如:必需=“真/假”
我的自定义属性定义:
public static readonly DependencyProperty AaFunctionalUnitNameProp;
[Category(VsCategoryName.AaObjectInfo)]
[Description(VsPropertyDescription.FunctionalUnitName)]
public string AaFunctionalUnitName
{
get => (string)GetValue(AaFunctionalUnitNameProp);
set => SetValue(AaFunctionalUnitNameProp, value);
}
答案 0 :(得分:1)
没有开箱即用的功能可以执行此操作,但是您可以分配无效(按照您的定义)默认值,并在OnInitialized
事件中抛出异常当它仍然是默认值时(仅当不在设计模式时)。
示例:
public class CustomControl : Control
{
public static readonly DependencyProperty RequiredPropertyProperty = DependencyProperty.Register(
"RequiredProperty", typeof(int), typeof(CustomControl), new PropertyMetadata(int.MinValue));
public int RequiredProperty
{
get { return (int) GetValue(RequiredPropertyProperty); }
set { SetValue(RequiredPropertyProperty, value); }
}
protected override void OnInitialized(EventArgs e)
{
if(RequiredProperty == int.MinValue)
if(!DesignerProperties.GetIsInDesignMode(this))
throw new Exception("RequiredProperty must be explicitly set!");
base.OnInitialized(e);
}
}