Unable to find enum type for static reference in WPF
Asked Answered
C

2

18

I'm trying to bind an enum to a radio button in WPF (Inspired by this answer), but I have trouble finding the enum type for the converter parameter:

The enum is defined in the following way

namespace Application.Models
{
    public class Enums
    {
        public enum MySelections { one, two ,three };

        public MySelections CurrentSelection;

        ...

    }
}

I am trying to bind now the checkbox like this (The data context is assumed to be correct and the value converter implemented:)

<Window x:Class="Application.MainWindow"
        ....
        xnlns:models="clr-namespace:Application.Models" >

...
<RadioButton Content="One"
             IsChecked="{Binding Path=CurrentSelection, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static models:Enums.MySelections.one}}" />
...

The problem lies with {x:Static models:Enums.MySelections.one} which constantly throws the error that the type models:Enums.MySelections could not be found.

How can I find my enum type?

Coco answered 15/4, 2011 at 12:22 Comment(1)
does it work if you move the enum definition up into the namespace and remove the Enum class from the bind?Scatology
S
51

Use "+" instead of "." to get to a nested type in XAML:

{x:Static models:Enums+MySelections.one}
Safier answered 15/4, 2011 at 12:37 Comment(3)
Except that in VS2010 at least that causes the designer to be unable to load the view, so then you can't even see what you are trying to design.Caseose
+1 - Works for me. However, it doesn't seem to work with x:Type. Does x:Type not support this syntax? (e.g. {x:Type models:Enums+MySelections})Matland
Great! This should be marked as the correct answer.Mckinney
I
17

You could declare it outside of your class:

namespace Application.Models
{
    public enum MySelections { one, two, three };

    public  class Enums
    {
        public MySelections CurrentSelection;

And then this xaml will work:

.... ConverterParameter={x:Static models:MySelections.one}

The x:Static markup has the fixed syntax:

{x:Static prefix:typeName.staticMemberName}

Idaho answered 15/4, 2011 at 12:30 Comment(1)
That was it, thank you. So just to clarify, where is this enum residing now? A static member of a namespace?Coco

© 2022 - 2024 — McMap. All rights reserved.