【问题标题】:How to bind in WPF a combo box to an enum from an other project?如何在 WPF 中将组合框绑定到来自其他项目的枚举?
【发布时间】:2014-08-08 15:50:53
【问题描述】:

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

在我的应用程序(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
  };
 }

我有这个错误:

命名空间“clr-namespace:Adapter”中不存在名称“MyEnum”。

我做错了什么?

【问题讨论】:

    标签: c# wpf combobox enums


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-12
      • 2020-02-07
      • 2011-08-04
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多