简体   繁体   中英

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. A class library and a WPF project. The class library "TemplateGeneratorLibrary" contains a file "Enums.cs" where I define my enums.

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

I've added the following reference to my view file in my UI project. I double checked that the assembly name of the class library is indeed "TemplateGeneratorLibrary" and is not different from the project name.

<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. And if the .NET Framework or . NET Core class library must comply with it.

Class library namespace does not exist in WPF app despite project reference

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.

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 .
  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.
  4. Select Add as link to add the common files(s) as a link into the project.

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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