Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find enum type for static reference in WPF

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?

like image 512
Christian Studer Avatar asked Apr 15 '11 12:04

Christian Studer


2 Answers

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

{x:Static models:Enums+MySelections.one}
like image 171
John Bowen Avatar answered Oct 24 '22 18:10

John Bowen


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}

like image 29
Dan Puzey Avatar answered Oct 24 '22 17:10

Dan Puzey