1

I have hit an odd situation in which an enum that is defined in a referenced assembly (dll that is a project in the same solution) is not found for use in my XAML. The same enum is found by C# code in my View Model.

When I copy the enum definition file into the project, the XAML references work, but the C# references, of course, complain about the duplicate definition. At this point I am at a loss as to how to resolve this issue. Any ideas? Known fixes?

Background:

  • The referenced assembly contains the enum, which is being used to back a XAML radio group, as recommended here.
  • I found this, but it does not apply to my situation. The enum in the referenced assembly stands on its own.
  • The error is thrown on the first {x:static } binding parameter in the xaml (example below) and reads "Cannot find the type 'MyEnum'. Note that type names are case sensitive."
  • Please excuse any minor mistakes in the below code, I typed it by hand since I can't disclose my company's production code.

EnumRadioGroupWindow.xaml

<Window x:Class="XamlWindow.EnumRadioGroupWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:XamlWindow"
    xmlns:enum="clr-namespace:EnumLibrary">
    <StackPanel>
        <StackPanel.Resources>
            <local:ComparisonToBoolConverter x:Key="ComparisonToBool"/>
        </StackPanel.Resources>
        <RadioButton Content="Item One" IsChecked="{Binding Path=InstanceOfMyEnum, Mode=OneWay, Converter={StaticResource ComparisonToBool}, ConverterParameter={x:Static enum:MyEnum.One}}"/>
        <RadioButton Content="Item Two" IsChecked="{Binding Path=InstanceOfMyEnum, Mode=OneWay, Converter={StaticResource ComparisonToBool}, ConverterParameter={x:Static enum:MyEnum.Two}}"/>
        <RadioButton Content="Item Three" IsChecked="{Binding Path=InstanceOfMyEnum, Mode=OneWay, Converter={StaticResource ComparisonToBool}, ConverterParameter={x:Static enum:MyEnum.Three}}"/>
    </StackPanel>
</Window>

MyEnum.cs (in separate assembly that is referenced by the WPF project)

namespace EnumLibrary
{
    public enum MyEnum
    {
        One,
        Two,
        Three
    }
}
Joshua W
  • 1,103
  • 12
  • 29
  • 1
    Double-check everything. Make sure you've actually added the library as a reference to your WPF project, make sure you've declared the XML namespace correctly (i.e. it should include the assembly name, like `xmlns:enum="clr-namespace:EnumLibrary;assembly=EnumLibrary"`...let the XAML editor help you with its auto-complete feature when you type in the namespace), and of course that it's really declared as `public` in the external assembly. I can't repro your problem unless I leave out the assembly name, so I suspect that's your issue. – Peter Duniho Apr 10 '20 at 00:27
  • Thanks, Peter! Adding the assembly hint did resolve the issue. The autocomplete in the XAML designer doesn't suggest the assembly hint! – Joshua W Apr 10 '20 at 18:11

1 Answers1

3

Did you try with adding the assembly name in the xmlns ?

xmlns:enum="clr-namespace:EnumLibrary;assembly=EnumLibrary"

I suggest to check again the assembly name of your EnumLibrary project. If you changed its name in the solution explorer, it doesn't mean it was updated in the project properties (xml node called <AssemblyName> in csproj).

Bonus, in XAML, if you need to use an enum which is embedded inside a class, it is done with + char

MyClass+MyEnum.MyCase
Nk54
  • 751
  • 7
  • 15