如何将自定义 属性 限制为 XAML 中的可用值

How to limit a custom property to available values in XAML

我有一个枚举声明如下:

public enum DirectionTypes
{
    IN = 2,
    OUT = 1
}

此枚举用于我需要在 XAML 中指定控件需要工作的方向的用户控件。我在每个用户控件上创建了一个依赖项 属性,如下所示:

public static readonly DependencyProperty DirectionTypeProperty =
        DependencyProperty.Register(
           "DirectionType",
           typeof(DirectionTypes),
           typeof(TransactionGrid), new PropertyMetadata(DirectionTypes.IN));

public DirectionTypes DirectionType
{
    get
    {
        return (DirectionTypes)GetValue(DirectionTypeProperty);
    }
    set
    {
        SetValue(DirectionTypeProperty, value);
    }
}

然后我可以按如下方式使用用户控件:

<local:TransactionGrid x:Name="theGrid" DirectionType="OUT" />

我可以运行这个程序就好了。问题是 DirectionType="OUT" 在 Visual Studio 2015 中导致了智能感知错误。我在 XAML 属性 下得到了蓝色波浪形,并且我的设计师不会显示预览,而是显示 "Invalid Markup"。错误显示 DirectionTypes 的类型转换器不支持从字符串转换。

我缺少什么可以让 XAML 正确解析。

明确指定枚举值如下(假设DirectionTypes与local同名space):

<local:TransactionGrid x:Name="theGrid" DirectionType="{x:Static local:DirectionTypes.OUT}" />