简体   繁体   English

如何使用另一个项目中的枚举将单选按钮组绑定到枚举属性?

[英]How can I bind a radio button group to an enum property using an enum from another project?

So I have 2 projects in my solution.所以我的解决方案中有 2 个项目。 A class library and a WPF project.一个类库和一个 WPF 项目。 The class library "TemplateGeneratorLibrary" contains a file "Enums.cs" where I define my enums.类库“TemplateGeneratorLibrary”包含一个文件“Enums.cs”,我在其中定义了我的枚举。

namespace TemplateGeneratorLibrary.Utilities
{
    public enum Genders
    {
        m,
        f
    }
}

I've added the following reference to my view file in my UI project.我在我的 UI 项目中添加了对我的视图文件的以下引用。 I double checked that the assembly name of the class library is indeed "TemplateGeneratorLibrary" and is not different from the project name.我仔细检查了类库的程序集名称确实是“TemplateGeneratorLibrary”并且与项目名称没有区别。

<UserControl x:Class="TemplateGeneratorUI.Views.MedicalExaminationView"
    xmlns:utilities="clr-namespace:TemplateGeneratorLibrary.Utilities;assembly=TemplateGeneratorLibary">

Now I'm trying to bind a radio button group to a property in my viewmodel using the enum from the class library.现在我正在尝试使用类库中的枚举将一个单选按钮组绑定到我的视图模型中的一个属性。

<RadioButton x:Name="PatientGenderMRadio"
    IsChecked="{Binding Path=Gender, Converter={StaticResource enumConverter}, ConverterParameter={x:Static utilities:Genders.m}}"

I'm getting the error "Cannot find the type 'Genders'. Note that type names are case sensitive."我收到错误消息“找不到类型‘性别’。请注意,类型名称区分大小写。”

When I put the enum file in the same project as the view everything works fine.当我将枚举文件与视图放在同一个项目中时,一切正常。 How can I get it to work using the enum from my class library though?我怎样才能使用我的类库中的枚举让它工作?

If you are using .NET Standard , the class library must also be built on the .NET Standard.如果您使用.NET Standard ,类库也必须基于 .NET Standard 构建。 And if the .NET Framework or .如果.NET Framework或 . NET Core class library must comply with it. NET Core类库必须遵守它。

Class library namespace does not exist in WPF app despite project reference 尽管有项目引用,但 WPF 应用程序中不存在类库命名空间

When I put the enum file in the same project as the view everything works fine.当我将枚举文件与视图放在同一个项目中时,一切正常。

One can actually bring in specific files from another project without having to bring in the reference to the project or include the dll.实际上可以从另一个项目中引入特定文件,而无需引入对项目的引用或包含 dll。

The trick is to include the file needed as a link into the project.诀窍是将所需的文件作为链接包含在项目中。 Here is how这是如何

  1. In the main project right click and select Add then Existing Item... or shift alt A .在主项目中右键单击并选择Add然后Existing Item...shift alt A
  2. Browse to the location of the file(s) found in the other project and select the file(s).浏览到在其他项目中找到的文件的位置并选择文件。
  3. Once the file(s) have been selected, then on the Add button uses the select in the drop down arrow.一旦选择了文件,然后在Add按钮上使用下拉箭头中的选择。
  4. Select Add as link to add the common files(s) as a link into the project.选择Add as link以将公共文件Add as link添加到项目中。

在此处输入图片说明

That will give access to the file as if the file was actually within the project, but the file physically resides elsewhere.这将允许访问该文件,就好像该文件实际上在项目中一样,但该文件实际上位于其他地方。


This was used extensively in Silverlight where one had to cross CLR boundaries, but things like models and enums could be used in both projects without having to pull in another CLRs dll just for textual type copies.这在必须跨越 CLR 边界的 Silverlight 中被广泛使用,但模型和枚举之类的东西可以在这两个项目中使用,而不必为了文本类型副本而引入另一个 CLR dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM