来自XAML的引用嵌套枚举类型

has*_*ock 10 c# xaml

我似乎无法从XAML引用公共嵌套枚举类型.我上课了

namespace MyNamespace
{
  public class MyClass
  {
    public enum MyEnum
    {
       A,
       B,
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试MyEnum像这样在Xaml中引用:

xmlns:MyNamespace="clr-namespace:MyNamespace;assembly=MyApp"
....

{x:Type MyNamespace:MyClass:MyEnum}    // DOESN'T WORK
Run Code Online (Sandbox Code Playgroud)

但VS抱怨它无法找到公共类型MyEnum.我也尝试使用+基于这篇文章的答案之一的语法...

{x:Type MyNamespace:MyClass+MyEnum}    // DOESN'T WORK
Run Code Online (Sandbox Code Playgroud)

但这也不起作用.

请注意,x:Static 适用于+语法:

{x:Static MyNamespace:MyClass+MyEnum.A}  // WORKS
Run Code Online (Sandbox Code Playgroud)

如果我MyEnum离开,MyClass我也可以参考它.但不是如果它是嵌套的......

那我错过了什么?如何使用XAML引用嵌套枚举x:Type?(注意,我不是试图实例化任何东西,只是引用类型).

UPDATE

看起来这只是VS 2010设计师的一个错误.设计师抱怨说Type MyNamespace:MyClass+MyEnum was not found.但应用程序似乎运行并正确访问嵌套类型.我也尝试使用嵌套类,它在运行时工作.

可能的开放式错误:http://social.msdn.microsoft.com/forums/en-US/wpf/thread/12f3e120-e217-4eee-ab49-490b70031806/

相关线程:在xaml中编写嵌套类型时的设计时错误

Dev*_*vin 3

这里有点晚了,但我使用了标记扩展,然后在我的 xaml 中使用以下引用来引用组合框中的嵌套枚举:

xmlns:MyNamespace="clr-namespace:MyNamespace;assembly=MyApp"
Run Code Online (Sandbox Code Playgroud)

...

ItemsSource="{Binding Source={resource:EnumBindingSource {x:Type MyNamespace:MyClass+MyEnum}}}"
Run Code Online (Sandbox Code Playgroud)

MarkupExtension 的代码取自此处

public class EnumBindingSourceExtension : MarkupExtension
{
    private Type _enumType;
    public Type EnumType
    {
        get { return this._enumType; }
        set
        {
            if (value != this._enumType)
            {
                if (null != value)
                {
                    Type enumType = Nullable.GetUnderlyingType(value) ?? value;
                    if (!enumType.IsEnum)
                        throw new ArgumentException("Type must be for an Enum.");
                }

                this._enumType = value;
            }
        }
    }

    public EnumBindingSourceExtension() { }

    public EnumBindingSourceExtension(Type enumType)
    {
        this.EnumType = enumType;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (null == this._enumType)
            throw new InvalidOperationException("The EnumType must be specified.");

        Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
        Array enumValues = Enum.GetValues(actualEnumType);

        if (actualEnumType == this._enumType)
            return enumValues;

        Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
        enumValues.CopyTo(tempArray, 1);
        return tempArray;
    }
}
Run Code Online (Sandbox Code Playgroud)