c# - 如何在 WPF 中将组合框绑定(bind)到其他项目的枚举?

标签 c# wpf combobox enums

这是我的问题。 我有一个包含组合框的 WPF 应用程序。 我想将此组合框绑定(bind)到其他项目的枚举。

在我的应用程序(AdapterApp)中,我尝试了以下操作:

        xmlns:System="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:Adapter"

    <Window.Resources>
        <ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues" 
                                    ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:MyEnum"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>

<ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}" HorizontalAlignment="Left" Margin="117,113,0,0" VerticalAlignment="Top" Width="150"/>

在我的其他项目中:

namespace Adapter
{
 public enum MyEnum
 {
    Lent = 0,
    Rapide
  };
 }

我有这个错误:

The name "MyEnum" does not exist in the namespace "clr-namespace:Adapter".

我做错了什么?

最佳答案

您应该添加对其他项目的引用,然后将 otherProjectNamespace 命名空间定义为 xmlns:otherProjectNamespace="clr-namespace:Adapter; assembly=MyOtherProject"

如果您未指定程序集,编译器会尝试在当前程序集(即应用程序的程序集)中查找 Adapter 命名空间。

关于c# - 如何在 WPF 中将组合框绑定(bind)到其他项目的枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24284048/

相关文章:

c# - Azure 文件存储 SMB 列出目录中的文件速度很慢

wpf - 当用户在其外部单击时如何关闭 WPF 对话框窗口

wpf - 在以编程方式添加的控件上使用 Storyboard 动画

wpf - 包含静态和动态数据的组合框

c# - 为什么组合框在创建时会加倍其项目?

c# - 隐式转换为 DateTime?和 op_Equality

c# - 支持 C# 和 F# 的最简单的 Visual Studio

javascript - 信号 R : How to send a complex object with XmppClient. 发送方法?

c# - 将 PNG 图像保存到内存流会导致错误

python - 列出带有子选项的 tkinter 组合框选项